473,388 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

where to use SQL Case statement ? and when ?and give example?

where to use SQL Case statement ? and when ?and give example?
Apr 22 '19 #1
2 1801
Luuk
1,047 Expert 1GB
You can use it to select even or odd numbers:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     i,
  3.     CASE WHEN i%2=1 THEN i ELSE 0 END  AS odd,
  4.     CASE WHEN i%2=0 THEN i ELSE 0 END  AS even
  5. FROM TEST
  6. WHERE i<1000
output:
Expand|Select|Wrap|Line Numbers
  1. i    odd    even
  2. 1    1    0
  3. 2    0    2
  4. 3    3    0
  5. 4    0    4
  6. 5    5    0
  7. 6    0    6
  8. 7    7    0
  9. 8    0    8
  10. 9    9    0
  11. 10    0    10
  12. 11    11    0
In simple cases (like above) there are some alternatives like IIF:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     i,
  3.     IIF (i%2=1, i, 0)  AS odd,
  4.     IIF (i%2=0, i, 0)  AS even
  5. FROM TEST
  6. WHERE i<12
but when expanding this example, you will no longer want to write this using IIF:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     i,
  3.     CASE i%4 
  4.            WHEN 3 THEN 3 
  5.            WHEN 2 THEN 2 
  6.            WHEN 1 THEN 1 
  7.            ELSE 0 END AS remainderAfterDivBy4
  8. FROM TEST
  9. WHERE i<12
output:
Expand|Select|Wrap|Line Numbers
  1. i    remainderAfterDivBy4
  2. 1    1
  3. 2    2
  4. 3    3
  5. 4    0
  6. 5    1
  7. 6    2
  8. 7    3
  9. 8    0
  10. 9    1
  11. 10    2
  12. 11    3
Apr 22 '19 #2
Rabbit
12,516 Expert Mod 8TB
Where?
Pretty much anywhere, in the select clause, or a join clause, or a where clause, or an order by clause, etc.

When?
Whenever you need to output different values based on a condition.

Examples?
See Luuk's response above.
Apr 22 '19 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Largo SQL Tools | last post by:
Can anyone tell me if it's possible to use a Case statement in a Where clause, and if so, the proper syntax? J.R. Largo SQL Tools The Finest Collection of SQL Tools Available...
1
by: mirth | last post by:
I would like to update a decimal column in a temporary table based on a set of Glcodes from another table. I search for a set of codes and then want to sum the value for each row matching the...
5
by: Rachel Weeden | last post by:
I'm working on an ASP Web application, and am having syntax issues in a WHERE statement I'm trying to write that uses the CInt Function on a field. Basically, I want to select records using...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
1
by: priyanka2203 | last post by:
Hi guys, I have a doubt regarding the CASE statement. It might sound silly, but me being new to DB2, it is kind of a genuine doubt. Try helping me with this.. When we use a case statement...
3
by: fanoftvb | last post by:
Hi, i have problem with my select case statement. When i click on the button, nothing happened. Can someone help me see where i go wrong, thanks a lot. :) Sub GridView1_RowCommand(ByVal sender...
1
by: Cloudbender | last post by:
how to create a program in c that shows your zodiac sign by using switch case statement. heres an example output: 1 - jan. 2 - feb. 3 - mar. 4 - apr. 5 - may 6 -...
1
by: hotflash | last post by:
Hi Master CroCrew et All, I am working on a CASE statement to allow different type of searches to search for different type of projects. EVERYTHING WORKS FINE EXCEPT, if the "Any Projects" radio...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
1
by: jetlight8 | last post by:
Hi , I have query with order by clause having customised column using case statement , when this column is used on order by clause , its showing error 'Invalid Column in the context' . If I try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.