Created
January 17, 2012 19:47
-
-
Save rodolfofadino/1628435 to your computer and use it in GitHub Desktop.
String Capitalization SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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