Capitalize First Character
65 pts.
0
Q:
Capitalize First Character
How can ensure that when certain columns in a row are INSERTed/UPDATEd, the first letter of the value is capitalized and the rest are lower case? For example "QUESTION" or "question" should be converted to "Question".
ASKED: Jun 16 2009  2:43 PM GMT
0
1410 pts.
0
A:
 RATE THIS ANSWER
+1
Click to Vote:
  •   1
  •  0
  • AddThis Social Bookmark Button
You could use a combination of a few functions. Upper, Left, Substring, Len & Lower

select upper(left('QUESTION',1)) + substring(Lower('QUESTION'),2,len('QUESTION'))

select upper(left('question',1)) + substring(Lower('question'),2,len('question'))

So you could make a function that passes in a parameter and returns your value so you don't have to put all these functions in your insert and update statments.


create function InitCap (@InValue varchar(255))
returns varchar
begin
declare @returnval varchar(255)
begin
set @returnval = select upper(left(@InValue,1)) + substring(Lower(@Invalue),2,len(@Invalue))
return @returnval
end
end


Now your insert or update statement can reference the function

Insert into table (col1, col2) values (InitCap('question'), InitCap('ANSWER'))

Inserted values should be Question and Answer
Last Answered: Jun 17 2009  2:17 PM GMT by Randym   1410 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



0