Convert a string into Title case in Sqlserver

Input: This is test input data!

output: This Is Test Input Data!

CREATE FUNCTION funTitleCase
(
@str VARCHAR(1000)
)
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE @counter INT = 0, @length INT = 0;
SET @length = LEN(@str);
SET @str = RTRIM(STUFF(@str,1,1,UPPER(SUBSTRING(@str,1,1))));
WHILE(@counter < @length)
BEGIN
IF(SUBSTRING(@str,@counter,1) =’ ‘ and SUBSTRING(@str,@counter+1,1) like ‘[a-zA-Z]’)
BEGIN
SET @str = STUFF(@str,@counter+1,1,UPPER(SUBSTRING(@str,@counter+1,1)));
END
SET @counter += 1;
END
RETURN ISNULL(@str,”);
END

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s