sqlsql-server

Set value of a column based upon another column


I want to set the value of ItemsSold.Description based on the value of ItemsSold.ReferenceID. The select statement will also have an alias for this field.

This is my SQL statement:

Select 
    ItemsSold.ItemID, 
    ItemsSold.ReferenceID as ReviewID,
    ItemsSold.Description as 'DisplayName' = Case When ReviewID = -1 Then
  'Waived'
    Else
        ItemsSold.Description 
    End,
    ItemsSold.PurchaseDate
From 
    ItemsSold
Where 
    ItemsSold.ItemID > 2000

This SQL throws an error at the "=" sign just before the Case expression as well as at the "," after End.

The datatype of both ItemsSold.Description and ItemsSold.ReferenceID is Varchar.


Solution

  • Something like this should work:

    Select ItemsSold.ItemID, 
           ItemsSold.ReferenceID as ReviewID, 
           Case When ItemsSold.ReferenceID = -1 
                Then   'Waived' 
                Else  ItemsSold.Description  
           End as DisplayName, 
           ItemsSold.PurchaseDate 
      From ItemsSold 
     Where ItemsSold.ItemID > 2000