Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
4.8k views
in SQL Queries by 11 11 15

How to use Nested if in Sql server

Here is the example

SELECT IF(Active = 'N' OR available = 'Y' ? 1 : 0) AS ExistInstore,* from store

 


1 Answer

0 like 0 dislike
by 24 26 40

To apply Nested-IF in SQL Server, you can use the Case statement instead of Multiple-If as below:

 SELECT CASE
         WHEN Active= 'N' or Available = 'Y'
               THEN 1
               ELSE 0
       END as Instore, * FROM Store

Use CAST if you want the result as a Boolean value.

SELECT CAST(

         CASE
              WHEN Active= 'N' or Available = 'Y'
                 THEN 1
              ELSE 0
         END AS bit) as Instore, * FROM Store

Nested-IF-Else in SQL Server Example

SELECT   
   CASE   
      WHEN value <= 0 THEN 0   
      WHEN value >= 100 THEN 1   
      WHEN value >= 1000 THEN 2   
      WHEN value >= 10000 THEN 3  
   END  as aresult
FROM Data ; 

You can see also

If you don’t ask, the answer is always NO!
...