microsoft:microsoft_sql_server:sql:format:initcap
Differences
This shows you the differences between two versions of the page.
microsoft:microsoft_sql_server:sql:format:initcap [2021/08/05 15:49] – created peter | microsoft:microsoft_sql_server:sql:format:initcap [2021/08/05 15:50] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Microsoft - Microsoft SQL Server - SQL - Format - InitCap ====== | ||
- | |||
- | <code sql> | ||
- | CREATE FUNCTION dbo.InitCap (@inStr VARCHAR(8000)) | ||
- | RETURNS VARCHAR(8000) | ||
- | AS | ||
- | BEGIN | ||
- | DECLARE @outStr VARCHAR(8000) = LOWER(@inStr), | ||
- | @char CHAR(1), | ||
- | @alphanum BIT = 0, | ||
- | @len INT = LEN(@inStr), | ||
- | @pos INT = 1; | ||
- | |||
- | -- Iterate through all characters in the input string | ||
- | WHILE @pos <= @len BEGIN | ||
- | |||
- | -- Get the next character | ||
- | SET @char = SUBSTRING(@inStr, | ||
- | |||
- | -- If the position is first, or the previous characater is not alphanumeric | ||
- | -- convert the current character to upper case | ||
- | IF @pos = 1 OR @alphanum = 0 | ||
- | SET @outStr = STUFF(@outStr, | ||
- | |||
- | SET @pos = @pos + 1; | ||
- | |||
- | -- Define if the current character is non-alphanumeric | ||
- | IF ASCII(@char) <= 47 OR (ASCII(@char) BETWEEN 58 AND 64) OR | ||
- | (ASCII(@char) BETWEEN 91 AND 96) OR (ASCII(@char) BETWEEN 123 AND 126) | ||
- | SET @alphanum = 0; | ||
- | ELSE | ||
- | SET @alphanum = 1; | ||
- | |||
- | END | ||
- | |||
- | | ||
- | END | ||
- | </ | ||
microsoft/microsoft_sql_server/sql/format/initcap.1628178555.txt.gz · Last modified: 2021/08/05 15:49 by peter