Technical Article

Change String to Title Case(Initcap)

,

Pass any string to give varriable @vString you want to convert to initcap. Execute the script your string changed to initcap.

--Simple code to change a string to a title case(initcap)
Declare @vString varchar(max)
Set @vString='put your string here in Any cASE'-- Put your string here


Declare @vLenCount int, @vWord varchar(max) 

Declare @vTable Table (StringCol varchar(max))

Select @vString = Lower(@vString), @vLenCount=len(@vString)



While @vLenCount > 1
Begin

Select @vWord=Substring(@vString, 0
, Case When charIndex(' ', LTRIM(RTRIM(@vString))) = 0 then Len(@vString)+1 Else charIndex(' ', LTRIM(RTRIM(@vString)))+1 End)


Insert into @vTable 

Select Upper(Substring(@vWord, 1,1))+ Lower(Substring(@vWord, 2, Len(@vWord)))                        

Select @vString = LTRIM(RTRIM(RIGHT(@vString, LEN(@vString) - LEN(@vWord))))

Select @vLenCount= Len(@vString)
End 

Select LTRIM(RTRIM((Select LTRIM(RTRIM(StringCol))+' ' from @vTable for xml path(''))))TitleCase

Rate

2 (5)

Share

Share

Rate

2 (5)