Below is just a quick and simple function to convert a short string to proper case. It's not the most efficient but will do its job for short strings.
The first character of the string is converted to upper case, subsequent characters that are preceded by a space are converted to upper case.
e.g. "this IS A TEST sentence. followed BY ANOTHER."
will be changed to
"This Is A Test Sentence. Followed By Another."
Function:
CREATE FUNCTION [dbo].[fncProperCase](@string VarChar(1000))
RETURNS VarChar(1000)
AS
BEGIN
DECLARE @pos int
DECLARE @len int
DECLARE @char VarChar(1)
DECLARE @temp VarChar(1000)
SET @string = LOWER(LTRIM(RTRIM(@string)))
SET @temp = ''
SET @pos = 1
SET @len = LEN(@string)
WHILE @pos <= @len
BEGIN
SET @char = SUBSTRING(@string, @pos, 1)
IF @pos = 1
BEGIN
SET @temp = @temp + UPPER(@char)
END
ELSE
BEGIN
IF SUBSTRING(@string, @pos - 1, 1) = ' '
BEGIN
SET @temp = @temp + UPPER(@char)
END
ELSE
BEGIN
SET @temp = @temp + @char
END
END
SET @pos = @pos + 1
END
RETURN @temp
END
Example:
SELECT dbo.fncProperCase('this IS A TEST sentence. followed BY ANOTHER.')
Result:
This Is A Test Sentence. Followed By Another.
Comments
Post a Comment