Finding Uppercase Character then Adding Space

后端 未结 3 769
梦毁少年i
梦毁少年i 2021-01-20 03:12

I bought a SQL World City/State database. In the state database it has the state names pushed together. Example: \"NorthCarolina\", or \"SouthCarolina\"...

IS there

3条回答
  •  无人及你
    2021-01-20 03:50

    Create this function

    if object_id('dbo.SpaceBeforeCaps') is not null
        drop function dbo.SpaceBeforeCaps
    GO
    create function dbo.SpaceBeforeCaps(@s varchar(100)) returns varchar(100)
    as
    begin
        declare @return varchar(100);
        set @return = left(@s,1);
        declare @i int;
        set @i = 2;
        while @i <= len(@s)
        begin
            if ASCII(substring(@s,@i,1)) between ASCII('A') and ASCII('Z')
                set @return = @return + ' ' + substring(@s,@i,1)
            else
                set @return = @return + substring(@s,@i,1)
            set @i = @i + 1;
        end;
        return @return;
    end;
    GO
    

    Then you can use it to update your database

    update tbl set statename = select dbo.SpaceBeforeCaps(statename);
    

提交回复
热议问题