Link to home
Start Free TrialLog in
Avatar of Westside2004
Westside2004Flag for United States of America

asked on

Conversion failed when converting the varchar value 'Not Relevant' to data type int.

Hi,

I am using SQL 2008.

I'm trying to write this query with a CASE statement and I'm getting this error:

Conversion failed when converting the varchar value 'Not Relevant' to data type int.

I understand why, because one column is of int datatype and the other is varchar.  How can I CAST/CONVERT this so the query runs successfully.

Here is my query that throws the error:

SELECT 
        Customer_ID,
        Customer_Level = 
		CASE Customer_Level
			WHEN '99' THEN CAST('Not Relevant' AS int)
			ELSE  Customer_Level END,
        First_Name
        FROM Customer

Open in new window


Customer_Level is a column with "int" datatype.

Any help appreciated....
ASKER CERTIFIED SOLUTION
Avatar of Habib Pourfard
Habib Pourfard
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can not cast 'No Relevant' to INT
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pourfard is right

You could do like

SELECT  Customer_ID,
        CASE Customer_Level
          WHEN 99 THEN 'Not Relevant'
          ELSE CAST(Customer_Level AS VARCHAR(20))
        END AS Customer_Level,
        First_Name
FROM    Customer
Avatar of Westside2004

ASKER

Thanks a bunch.  Worked...