I needed to quickly (and dirtily) transform rows which were all upper case into pascal cased, so 'I AM OPIE' become 'I Am Opie'.
Here's something I found and ported to a sql function:
CREATE FUNCTION [dbo].[Casing]
(@instring VARCHAR(255))
RETURNS varchar(1000) AS
BEGIN
--usage: SELECT dbo.Casing('I AM A DOG')
DECLARE @strptr INT
DECLARE @strChar char(1)
DECLARE @outstring varchar(1000)
SET @outstring = ''
SET @strptr = 0
WHILE @strPtr < len(RTRIM(@instring))
BEGIN
SET @strptr = @strptr + 1
SET @strchar = SUBSTRING(@instring,@strptr,1)
IF @strptr = 1
SET @outstring = UPPER(@strchar)
ELSE
IF SUBSTRING(@instring,(@strptr - 1),1) = ' '
or SUBSTRING(@instring,(@strptr - 1),1) = ''''
SET @outstring = SUBSTRING(@outstring,1,@strptr) +
UPPER(@strchar)
ELSE
SET @outstring = SUBSTRING(@outstring,1,@strptr) +
LOWER(@strchar)
END
RETURN @outstring
END
Posted
03-27-2009 10:07 AM
by
Michael Nichols