views:

23

answers:

1

Hello,

I have a view in SQL server that translates from one schema version to another.
Currently, the view looks like this:

SELECT newValue AS oldValue  
FROM dbo.MyTable

The trouble is that, in the new schema, newValue is not nullable, so we set it to -1 to denote empty fields, but in the old schema, it was nullable.

How can I do something to the effect of:

SELECT  
(  
  IF( newValue > -1 )  
    newValue as oldValue  
  ELSE  
    NULL as oldValue
)  
FROM dbo.MyTable
+6  A: 
SELECT  
  case when newValue > -1 then  
    newValue  
  else  
    NULL
  end as oldValue
FROM dbo.MyTable
Frank
+1: Beat me by 11 seconds
OMG Ponies
Awesome, thanks so much :) I'll flag as answer as soon as it lets me.
b34r
How does this compare speed-wise Vs COALESCE ?
adolf garlic
@adolf I would assume that there is not a huge difference. In SQL, normally more than 90 percent of the speed is related to data access strategy. The time for calculating something within a record can normally just be neglected.
Frank
@adolf How do you want to use `coalesce()` here? The intent is to convert something TO null, not FROM null, as `coalesce()` would do.
Frank