case when issue

5 pts.
Tags:
case when expression
Conditional expressions
SQL Server
T-SQL
I recently came across behavior of a "case when" conditional expression that really confuses me. In this case: -- CASE when isnumeric('-10.00%') > 0 then convert(numeric, '-10.00%') end ---- the behavior is as I expected - the isnumeric() function returns 0 (cuz it ain't no number) so the "then" is not evaluated and a null is returned. HOWEVER, if you simply add an else to the case when expression then there is a failed varchar to numeric conversion. So... ---- CASE when isnumeric('-10.00%') > 0 then convert(numeric, '-10.00%') else 'Not Numeric' end ---- bombs out with error message "Error converting data type varchar to numeric." Hmmm, that sure does confuse me! If anyone knows what is wrong with my understanding of the behavior I would be ever-so-grateful for an explanation. Thank you kindly, this one has me reelin'! zh
ASKED: November 17, 2007  1:39 AM
UPDATED: January 29, 2008  1:57 AM

Answer Wiki

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

You are getting this because the data within the THEN portion of the CASE statement is a number. You would need to put a numeric value into the ELSE portion. Your other option would be to leave the THEN value as a text string.

A single column can only have a single data type. “convert(numeric, ‘-10.00%’)” is a number, while ‘Not Numeric’ is a string.

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