Reputation: 15200
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?
Upvotes: 59
Views: 592633
Reputation: 10226
SELECT IF(COLUMN_NAME = "VALUE", "VALUE_1", "VALUE_2") AS COLUMN_NAME
FROM TABLE_NAME;
OR
SELECT (CASE WHEN COLUMN_NAME = "VALUE" THEN 'VALUE_1' ELSE 'VALUE_2' END) AS COLUMN_NAME
FROM TABLE_NAME;
Upvotes: 7
Reputation: 60913
Here is some example using CASE WHEN
SELECT
CASE WHEN A > 1 THEN
'Greater than 1'
END
FROM TRIANGLES
.
SELECT
CASE WHEN A > 1 THEN
A
END
FROM TRIANGLES
.
SELECT
CASE WHEN A > 1 and B > 1 THEN
'Greater than 1'
END
FROM TRIANGLES
.
SELECT
CASE WHEN A > 1 THEN
'greater than 1'
ELSE
'less than 1'
END
FROM TRIANGLES
.
SELECT
CASE WHEN A > 1 THEN
'greater than 1'
ELSE CASE WHEN A >= 0 THEN
'greater than or equal 0'
ELSE
'less than 0'
END
END
FROM TRIANGLES;
Hope this helps
Upvotes: 3
Reputation: 186
You can use simply if
statement under select
query as like I have described below
if(some_condition,if_satisfied,not_satisfied)
SELECT IF(IDParent < 1,ID,IDParent) FROM yourTable ;
Upvotes: 2
Reputation: 19
I Have a Query With This result :
SELECT Top 3
id,
Paytype
FROM dbo.OrderExpresses
WHERE CreateDate > '2018-04-08'
The Result is :
22082 1
22083 2
22084 1
I Want Change The Code To String In Query, So I Use This Code :
SELECT TOP 3
id,
CASE WHEN Paytype = 1 THEN N'Credit' ELSE N'Cash' END AS PayTypeString
FROM dbo.OrderExpresses
WHERE CreateDate > '2018-04-08'
And Result Is :)
22082 Credit
22083 Cash
22084 Credit
Upvotes: 0
Reputation: 5588
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
Upvotes: 36
Reputation: 173
sql server 2012
with
student as
(select sid,year from (
values (101,5),(102,5),(103,4),(104,3),(105,2),(106,1),(107,4)
) as student(sid,year)
)
select iif(year=5,sid,year) as myCol,* from student
myCol sid year
101 101 5
102 102 5
4 103 4
3 104 3
2 105 2
1 106 1
4 107 4
Upvotes: 3
Reputation: 51
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]
Upvotes: 5
Reputation: 13486
SELECT CASE WHEN IDParent < 1
THEN ID
ELSE IDParent
END AS colname
FROM yourtable
Upvotes: 23
Reputation: 13785
select
CASE WHEN IDParent is < 1 then ID else IDParent END as colname
from yourtable
Upvotes: 0
Reputation: 3704
You can also use a union construct. I'm not sure if CASE is a common SQL construct ...
SELECT ID FROM tabName WHERE IDParent<1 OR IDParent IS NULL
UNION
SELECT IDParent FROM tabName WHERE IDParent>1
Upvotes: 0
Reputation: 263693
you can use CASE
SELECT ...,
CASE WHEN IDParent < 1 THEN ID ELSE IDPArent END AS ColumnName,
...
FROM tableName
Upvotes: 117