Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Created January 17, 2012 19:47
Show Gist options
  • Save rodolfofadino/1628435 to your computer and use it in GitHub Desktop.
Save rodolfofadino/1628435 to your computer and use it in GitHub Desktop.
String Capitalization SQL
CREATE FUNCTION Captalize( @InputString varchar(max))
RETURNS varchar(max)
AS
BEGIN
DECLARE @Index INT
DECLARE @Char CHAR(1)
DECLARE @PrevChar CHAR(1)
DECLARE @OutputString VARCHAR(max)
SET @OutputString = LOWER(@InputString)
SET @Index = 1
WHILE @Index <= LEN(@InputString)
BEGIN
SET @Char = SUBSTRING(@InputString, @Index, 1)
SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
ELSE SUBSTRING(@InputString, @Index - 1, 1)
END
IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
BEGIN
IF @PrevChar != '''' OR UPPER(@Char) != 'S'
SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
END
SET @Index = @Index + 1
END
Return @OutputString
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment