Home > SQL, SQL 2005, SQL 2008 > SQL Proper Casing

SQL Proper Casing

On one of my recent project I got a requirement to proper case a SQL table column values. The data got into this table is through an SSIS package from a 3rd party database. To tackle this I created a user defined function

CREATE FUNCTION ProperCase(@inpStr nvarchar(8000))
RETURNS nvarchar(8000) AS
BEGIN
     DECLARE @Result nvarchar(8000)
     SET @inpStr = LOWER(@inpStr) + ' '
     SET @Result = ''
     WHILE 1=1
     BEGIN
        IF PATINDEX('% %',@inpStr) = 0 BREAK
        SET @Result = @Result + UPPER(Left(@inpStr,1))+
        SubString  (@inpStr,2,CharIndex(' ',@inpStr)-1)
        SET @inpStr = SubString(@inpStr, CharIndex(' ',@inpStr)+1,Len(@inpStr))
     END
     SET @Result = Left(@Result,Len(@Result))
     RETURN @Result
END
Advertisements
Categories: SQL, SQL 2005, SQL 2008
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: