sqlsql-server

Using If else in SQL Select statement


I have a select statement which will return 2 columns.

ID | IDParent

Then in my program I have to test if IDParent is < 1 then use ID ELSE use IDParent

Is there a way to use an If Else like condition in SQL to return only single value?


Solution

  • you can use CASE

    SELECT   ...,
             CASE WHEN IDParent < 1 THEN ID ELSE IDPArent END AS ColumnName,
             ...
    FROM     tableName