Created
July 1, 2015 15:28
-
-
Save ststeiger/553a51983c7c970487b7 to your computer and use it in GitHub Desktop.
Text to title-case 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
| IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[udf_TitleCase]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) | |
| DROP FUNCTION [dbo].[udf_TitleCase] | |
| GO | |
| CREATE FUNCTION [dbo].[udf_TitleCase] (@InputString VARCHAR(4000) ) | |
| RETURNS VARCHAR(4000) | |
| AS | |
| BEGIN | |
| -- Credits: | |
| -- http://blog.sqlauthority.com/2007/02/01/sql-server-udf-function-to-convert-text-string-to-title-case-proper-case/ | |
| DECLARE @Index INT | |
| DECLARE @Char CHAR(1) | |
| DECLARE @OutputString VARCHAR(255) | |
| SET @OutputString = LOWER(@InputString) | |
| SET @Index = 2 | |
| SET @OutputString = | |
| STUFF(@OutputString, 1, 1,UPPER(SUBSTRING(@InputString,1,1))) | |
| WHILE @Index <= LEN(@InputString) | |
| BEGIN | |
| SET @Char = SUBSTRING(@InputString, @Index, 1) | |
| IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&','''','(') | |
| IF @Index + 1 <= LEN(@InputString) | |
| BEGIN | |
| IF @Char != '''' OR UPPER(SUBSTRING(@InputString, @Index + 1, 1)) != 'S' | |
| SET @OutputString = STUFF(@OutputString, @Index + 1, 1,UPPER(SUBSTRING(@InputString, @Index + 1, 1))) | |
| END | |
| SET @Index = @Index + 1 | |
| END | |
| RETURN ISNULL(@OutputString,'') | |
| END | |
| GO | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment