Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case When Statement

Status
Not open for further replies.

saw15

Technical User
Jan 24, 2001
468
0
0
US
I am pulling data off a table, where sometimes the data is null and sometimes it is not.

I currently use the following in a select statement, and it errors out when I have a null value.

select convert(numeric(15,1),Qualify)
from tablename

If there is a value, it works fine. If it is null, I get an error.

I need a case statement to determine when its null, and when its not use the convert use do nothing or populate the value with a blank (not sure if T-SQL uses the '').

Help is much appreciated.
 
There is a function called ISNULL that will return a value specified in the query when the row is null.

ISNULL ( check_expression , replacement_value )
 
To make things a little more complicated...

The value coming in is a varchar(15), the value going out must be a numeric value. So I need to do a convert, where would I apply this?

isnull(convert(numeric(15,1),qualify),0) from tablename

gives the following error:
Server: Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.

So I thought a case when would do this, any other ideas?
 
Hi saweens,

Here's the syntax you'll need...

Code:
convert(numeric(15,1),isnull(qualify,0))

HTH,

MapMan

Assume nothing, question everything, be explicit not implicit, and you'll always be covered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top