using IF in a SQL Server View

11,441
SELECT  
  case when newValue > -1 then  
    newValue  
  else  
    NULL
  end as oldValue
FROM dbo.MyTable
Share:
11,441
b34r
Author by

b34r

Updated on June 04, 2022

Comments

  • b34r
    b34r almost 2 years

    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