How do I perform an IF THEN statement in an SQL SELECT

0 votes

How to execute an IF...THEN in an SQL SELECT statement

For example:

SELECT IF(Accept = 'Y' OR Reject = 'N' ? 1 : 0) AS Approved, * FROM Sales
Feb 16 in Database by Neha
• 8,560 points
61 views

1 answer to this question.

0 votes

The CASE statement is the closest to IF in SQL and is supported on all versions of SQL Server.

SELECT CAST(
             CASE
                  WHEN Accept = 'Y' or Reject = 'N'
                     THEN 1
                  ELSE 0
             END AS bit) as Approved, *
FROM Sales

You only need to use the CAST operator if you want the result as a Boolean value. If you are happy with an int, this works:

SELECT CASE
            WHEN Accept = 'Y' or Reject = 'N'
               THEN 1
               ELSE 0
       END as Approved, *
FROM Sales

CASE statements can be embedded in other CASE statements and even included in aggregates.

SQL Server Denali (SQL Server 2012) adds the IIF statement which is also available in access (pointed out by Martin Smith):

SELECT IIF(Accept = 'Y' or Reject = 'N', 1, 0) as Approved, * FROM Sales
answered Feb 16 by Vaani
• 7,020 points

Related Questions In Database

0 votes
1 answer

how do I calculate discount with if function in excel?

Suppose a customer gets a 10 percent ...READ MORE

answered Feb 23 in Database by gaurav
• 13,460 points
609 views
0 votes
1 answer

how do I calculate discount with if function in excel

Notes about the release; Frameworks to aim ...READ MORE

answered Mar 25 in Database by gaurav
• 13,460 points
46 views
0 votes
2 answers

How to select the nth row in a SQL database table?

SELECT * FROM ( SELECT ID, NAME, ROW_NUMBER() ...READ MORE

answered Apr 23, 2020 in Database by anand
• 140 points
16,166 views
0 votes
1 answer

How do I reset a sequence in Oracle?

 If you want to set it to ...READ MORE

answered Sep 27, 2019 in Database by Omaiz
• 560 points
321 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4 in Database by Vaani
• 7,020 points
20 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4 in Database by Neha
• 8,560 points
116 views
0 votes
2 answers

Case in Select Statement

I think these could be helpful for ...READ MORE

answered Feb 4 in Database by Neha
• 8,560 points
55 views
0 votes
1 answer

LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

At the top level there are mainly ...READ MORE

answered Feb 4 in Database by Neha
• 8,560 points
89 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3 in Database by Vaani
• 7,020 points
56 views
0 votes
1 answer

How can I define a composite primary key in SQL?

A primary key is unique and it ...READ MORE

answered Feb 21 in Database by Vaani
• 7,020 points
73 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP