Mobiwan 21.09.2214:08

How can I use IF...THEN in an SQL SELECT?

How do I perform an IF...THEN in an SQL SELECT statement?
https://www.scaler.com/topics/sql/?
For example:[code]

SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
[/code]
I have tried to look up on various article like this one but I can't understand.
This is my code
[code]
IF @IncludeExtendedInformation = 1
BEGIN
SELECT A,B,C,X,Y,Z
FROM T
END
ELSE
BEGIN
SELECT A,B,C
FROM T
END

[/code]
Mad Hatter 21.09.2214:33
1. Here you go בתגובה להודעה מספר 0
SELECT *, IF(Obsolete = 'N' OR InStock = 'Y', 1, 0) AS Saleable FROM Product

You have two mistakes.
1 The syntax of the if conduction, You shouldn't treat the if conduction like a ternary operator.

https://www.w3schools.com/sql/func?_...

2 The asterisk should come before the if condition, not after.
Pascual 26.09.2213:46
2. The correct syntax for case when.. בתגובה להודעה מספר 0

ערכתי לאחרונה בתאריך 26.09.22 בשעה 13:51 בברכה, Pascual

Hey, it's important to understnad to which dialect your data terminal is set to.
could be set to postgres, mysql, snowflake etc..

CASE statement is the equivalent to IF in many sql dialects.

the most frequent one when talking in sql is

CASE
WHEN X then Y
WHEN Z then G
WHEN A then B
ELSE K
END

as 'CaseColumnName'

this is as if i would write:

IF
X THEN Y
ELSEIF Z THEN G
ELSEIF A THEN B
ELSE K
END

as 'IfColumnName'

@Mobiwan@
העבר לפורום אחר
העבר לפורום:
סיבה:
תגובה חדשה
כותרת:
תוכן:
סמיילים:
הצג
עריכת אשכול
כותרת:
תוכן:
סמיילים:
הצג