| |
Welcome to the dBforums forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact contact support.
If you prefer not to see double-underlined words and corresponding ads, place your cursor here for ContentLink opt out.
|
 |

12-14-07, 03:32
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Chennai,India
Posts: 11
|
|
|
any way to modify this CASE statement
|
Hello all,
Is there any other way(without using CASE) to modify this query?
SELECT CASE
WHEN type IN (21,22) THEN 16
WHEN type IN (37,38) THEN 32
WHEN type IN (69,70) THEN 64
WHEN type IN (133,134) THEN 128
WHEN type IN (261,262) THEN 256
WHEN type IN (517,518) THEN 512
WHEN type IN (1029,1030) THEN 1024
AS xxx,
Please note all the xxx values are in powers of two.
Thanks in advance.
|
Last edited by O'sambo : 12-14-07 at 05:08.
|

12-14-07, 07:57
|
|
Registered User
|
|
Join Date: May 2005
Location: South Africa
Posts: 830
|
|
What are you trying to do? Your case statement is incomplete (no end).
You do not have an else statement. Do you only select these types in the where clause or do you display nulls for other types
|
|

12-14-07, 10:26
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Chennai,India
Posts: 11
|
|
|
The column 'type' will have only those values(21,22,37,38,69,70,133,134,261,262,517,518,1 029,1030).I have edited the query
SELECT
type,
CASE
WHEN type IN (21,22) THEN 16
WHEN type IN (37,38) THEN 32
WHEN type IN (69,70) THEN 64
WHEN type IN (133,134) THEN 128
WHEN type IN (261,262) THEN 256
WHEN type IN (517,518) THEN 512
WHEN type IN (1029,1030) THEN 1024
END AS xxx
From table_name
I am just looking for another way to achieve the above result without using CASE
|
|

12-14-07, 11:58
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,555
|
|
SELECT type, (type/16)*16 AS xxx FROM table_name
|
|

12-17-07, 04:43
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Chennai,India
Posts: 11
|
|
many thanks to everyone;
Also SELECT type,type & 2032 AS xxx FROM table_name gives the same result.
This is how it have been written in my project.The guy who had did this left the company.
I would be happy if you tell me how he had arrived to the number 2032.
Thanks in advance.
|
|

12-18-07, 10:41
|
|
Registered User
|
|
Join Date: Feb 2007
Location: Chennai, TN
Posts: 59
|
|
Hi,
The logic to get the number 2032 is by adding 16+32+64+128+256+512+1024 = 2032
The added numbers are the values after your THEN statement in your first post
The logic is quit simple, you need to get the results as 2^4, 2^5, 2^6. 2^7, 2^8, 2^9 and 2^10 based on the condition
The binary value of 2032 = 0111 1111 0000, which means that the value 1 will be available in all the location that corresponds to 16 (2^4), 32 (2^5), 64(2^6), 128 (2^7), 256 (2^8), 512 (2^9) and 1024 (2^10). Thats why he should have choosen 2032
Now for example, lets take your 1st condition to check 21,
binary value of 21 = 0000 0001 0101
binary value of 2032 = 0111 1111 0000
---------------
21 & 2032 = 0000 0001 0000
----------------
0000 0001 0000 will correspond to the value 16 which is your required result. 1 will be available in only 1 place in the result of the above binary
I will take another example using another value of yours, let it be 70
binary value of 70 = 0000 0100 0110
binary value of 2032 = 0111 1111 0000
---------------
70 & 2032 = 0000 0100 0000
---------------
0000 0100 0000 will correspond to the value 64 which is your required result for the case 70
note : ^ refers to power
|
|

12-19-07, 01:46
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Chennai,India
Posts: 11
|
|
 that is a wonderful explanation ..
Thank you very much.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|