Combine Lower() Upper() and substring() function to capital the first letter in a word : UPPER « String Functions « SQL Server / T-SQL






Combine Lower() Upper() and substring() function to capital the first letter in a word


1>
2> -- LOWER() and UPPER() Functions
3> DECLARE @LastName VarChar(25), @SpaceIndex TinyInt
4> SET @LastName = 'mc donald'
5>
6> -- Find space in name:
7> SET @SpaceIndex = CHARINDEX(' ', @LastName)
8> IF @SpaceIndex > 0                       -- Space: Capitalize first & substring
9>       SELECT UPPER(LEFT(@LastName, 1))
10>     + LOWER(SUBSTRING(@LastName, 2, @SpaceIndex - 1))
11>     + UPPER(SUBSTRING(@LastName, @SpaceIndex + 1, 1))
12>     + LOWER(SUBSTRING(@LastName, @SpaceIndex + 2, LEN(@LastName)))
13> ELSE                                     -- No space: Cap only first char.
14>       SELECT UPPER(LEFT(@LastName, 1))
15>     + LOWER(SUBSTRING(@LastName, 2, LEN(@LastName)))
16>
17> GO

----------------------------------------------------
Mc Donald

(1 rows affected)
1>
           
       








Related examples in the same category

1.UPPER: change string to upper case
2.UPPER: Translates all characters within the string to upper case