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?


you can use CASE

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

Here, using CASE Statement and find result:

select (case when condition1 then result1
             when condition2 then result2
             else result3
             end) as columnname from tablenmae:

For example:

select (CASE WHEN IDParent< 1 then ID 
             else IDParent END) as columnname
from tablenmae

SELECT CASE WHEN IDParent < 1 
            THEN ID 
            ELSE IDParent 
       END AS colname 
FROM yourtable

The query will be like follows

SELECT (CASE WHEN tuLieuSo is null or tuLieuSo=''
            THEN 'Chưa có đĩa' 
            ELSE 'Có đĩa' End) AS tuLieuSo,moTa
FROM [gPlan_datav3_SQHKTHN].[dbo].[gPlan_HoSo]