0

I have a simple select statement below:

SELECT Code =   Cast([Code] as int)
FROM dbo.table 

I get an error:

Conversion failed when converting the nvarchar value '?' to data type int.

I want to write IFERROR or CASE statement that would would replace error with a number such as -9999

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
BI Dude
  • 1,842
  • 5
  • 37
  • 67

2 Answers2

5
SELECT 
    Code = CASE 
               WHEN ISNUMERIC(Code) = 1 THEN Cast([Code] as int) 
               ELSE -9999 
    END
FROM 
    dbo.table
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M. Page
  • 2,694
  • 2
  • 20
  • 35
0
SELECT Code = Cast((case when [Code] = '?' then -9999 else [Code] end) as int)
FROM dbo.table 
juergen d
  • 201,996
  • 37
  • 293
  • 362