Author |
Topic |
vidhya
Posting Yak Master
108 Posts |
Posted - 2009-04-14 : 05:16:09
|
hi friends, i need query to make the first letter of a word as capital.I am having field name. The names first letter should be caps.inserting values in the table may be any either caps or small letter. but displaying should be first letter caps.Thanks in advance, |
|
shekhar
Starting Member
3 Posts |
Posted - 2009-04-14 : 05:25:50
|
declare @test nvarchar(200)set @test = 'test cases'select UPPER(substring(@test,1,1)) + SUBSTRING(@test,2,len(@test)-1) |
|
|
vidhya
Posting Yak Master
108 Posts |
Posted - 2009-04-14 : 05:31:20
|
thanks sekhar |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-04-14 : 06:19:23
|
Here is a proper case function for you which do the Propercase tasks to a string...CREATE FUNCTION dbo.ProperCase(@string VARCHAR(255))RETURNS VARCHAR(255) ASBEGIN– DeclarationsDECLARE @return VARCHAR(255)DECLARE @len INTDECLARE @finished BITDECLARE @pos INTDECLARE @thischar CHAR(1)DECLARE @thisasc INTDECLARE @capitalise BIT– InitialiseSELECT @finished = 0SELECT @string = LOWER(@string)SELECT @return = @stringSELECT @len = DATALENGTH(@return)– FailsafeIF @len = 0SELECT @finished = 1– Loop over all characters: capitalise first character and those after spaces, replace underscores with spacesSELECT @pos = 0SELECT @capitalise = 1WHILE @finished = 0BEGIN– Next position in stringSELECT @pos = @pos + 1– Done?IF @pos > @lenSELECT @finished = 1ELSEBEGIN– Read this characterSELECT @thischar = SUBSTRING(@return, @pos, 1)SELECT @thisasc = ASCII(@thischar)IF @thischar IN ('_', ' ')BEGINSELECT @return = LEFT(@return, @pos - 1) + ' ' + RIGHT(@return, @len - @pos)SELECT @capitalise = 1ENDELSEBEGINIF @capitalise = 1 AND (@thisasc BETWEEN 97 AND 122)BEGINSELECT @thisasc = @thisasc - 32SELECT @return = LEFT(@return, @pos - 1) + CHAR(@thisasc) + RIGHT(@return, @len - @pos)SELECT @capitalise = 0ENDENDENDEND– DoneRETURN @returnENDGOselect dbo.PropCase ('ashish gilhotra wat are you doing')Thanks May be like that u want |
|
|
mase2hot
Starting Member
36 Posts |
Posted - 2010-04-18 : 10:56:06
|
Hi,how do I get the function to run against a colum in my table? I tried execute dbo.ProperCase (select first_name from dbo.clients)but get error:Procedure or function 'PROPERCASE' expects parameter '@input', which was not supplied. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-18 : 13:03:04
|
select dbo.ProperCase(first_name) from dbo.clients No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
mase2hot
Starting Member
36 Posts |
Posted - 2010-04-18 : 13:24:58
|
thanks for that worked instantly! How do I commit the changes though to the table? Sorry I'm used to using Oracle.... |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-18 : 13:27:42
|
You want to update your table?In your first post I see: but displaying should be first letter caps No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
mase2hot
Starting Member
36 Posts |
Posted - 2010-04-18 : 14:06:47
|
thanks worked a treat! |
|
|
|