Capitalize First Character

110 pts.
Tags:
INSERT statement
T-SQL
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".

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following