Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Uppercase Character then Adding Space

Tags:

sql

uppercase

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 a way in SQL to loop and find the uppercase characters and add a space???

this way "NorthCarolina" becomes "North Carolina"???

like image 890
mrjamiebowman Avatar asked Oct 15 '25 03:10

mrjamiebowman


1 Answers

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);
like image 50
RichardTheKiwi Avatar answered Oct 17 '25 19:10

RichardTheKiwi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!