Link to home
Start Free TrialLog in
Avatar of chaverly
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
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

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.
Avatar of chaverly

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
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thank you for getting back to me.  I really appreciate your help.