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?
In MS SQL, IF…ELSE is a type of Conditional statement. Any T-SQL statement can be executed conditionally using IF… ELSE. If the condition evaluates to True, then T-SQL statements followed by IF condition in SQL server will be executed.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With