chaverly
asked on
Want to use if then instead of Case end in Transact SQL
Using Transact SQL in SQL SERVER Management studio. I have always used a Case statement when choosing. Can I use an If then
Example:
SELECT [Order ID],
[Order Amount],
[Order date],
if shipped = 1 then
'yes'
else
'no'
End as shipStatus
FROM Orders
WHERE [Order ID] between 2934 and 2944
Example:
SELECT [Order ID],
[Order Amount],
[Order date],
if shipped = 1 then
'yes'
else
'no'
End as shipStatus
FROM Orders
WHERE [Order ID] between 2934 and 2944
CASE is the same thing as IF-THEN-ELSE. Why would you want to use a different syntax?
You just cannot embed a IF into a SQL statement. IF can only be used for control-flow. https://docs.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql?view=sql-server-2017
Not sure why you would want to do so, when CASE is so easy to use.
ASKER
Am teaching a class and student remarked that they had seen an if then statement in a report they inherited.
maybe in a SP or a function but in a SQL Query per say
Maybe an IIF statement instead of a normal IF?
https://docs.microsoft.com/en-us/sql/t-sql/functions/logical-functions-iif-transact-sql?view=sql-server-2017
https://docs.microsoft.com/en-us/sql/t-sql/functions/logical-functions-iif-transact-sql?view=sql-server-2017
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use many functions that are more aligned with VB or VBA than with SQL Server within expressions in SSRS.
I found it very confusing when I started working with SSRS.
I found it very confusing when I started working with SSRS.
ASKER
Thank you for getting back to me. I really appreciate your help.