SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Equivalent of 'DECODE' in sql


Equivalent of 'DECODE' in sql

Author
Message
kiran-670359
kiran-670359
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 66
Hi all,

Smile Can anyone provide me with correct syntax for' 'DECODE' function equivalent in sql.

Thanks,
kiran.

Kiran
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6481 Visits: 25549
Use
CASE ... WHEN ... THEN ... ELSE ... END

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




kiran-670359
kiran-670359
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 66
i have a table called university which have two fields id and name.

i tried this way.

SELECT CASE u.id
WHEN 2 THEN 'GOOD'
WHEN 3 THEN 'POOR'
WHEN 4 THEN 'EXCELLENT'
ELSE 'BAD'
END CASE
from university u;

it is not executing.can u suggest me where my query is wrong.....

Kiran
gan-685682
gan-685682
SSC Veteran
SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)

Group: General Forum Members
Points: 289 Visits: 215
Should be something like this (http://msdn2.microsoft.com/en-us/library/aa258235(SQL.80).aspx) ?
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
Key DBA
Key DBA
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1723 Visits: 655
Kiran,

You were very close to getting your code to run. The line "END CASE" should just be "END". I wrote a test case demonstration, based on your information.


-- Create university Table.
CREATE TABLE dbo.university
(
[id] NUMERIC(10,0)
); NUMBER(10,0)
);

-- Insert sample data.
INSERT INTO university VALUES (1);
INSERT INTO university VALUES (2);
INSERT INTO university VALUES (3);
INSERT INTO university VALUES (4);
INSERT INTO university VALUES (5);

-- Select the data as a preview.
select [id] from university

-- Build the case statement with the following Rules:
-- ID of 2 = GOOD
-- ID of 3 = POOR
-- ID of 4 = EXCELLENT
-- all other ID values default to = BAD
SELECT
[id], -- Display the original value for [id].
CASE u.[id]
WHEN 2 THEN 'GOOD'
WHEN 3 THEN 'POOR'
WHEN 4 THEN 'EXCELLENT'
ELSE 'BAD'
END AS 'GRADE_REMARK' -- this is just a column label
FROM dbo.university AS u


-- Clean up the demonstration.
-- Drop the university table.
DROP TABLE dbo.university




Let me know if this is what you were expecting.

Best Regards,

"Key"
MCITP: DBA, MCSE, MCTS: SQL 2005, OCP
kiran-670359
kiran-670359
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 66
Thanks Wilson,

It's working Smile.

Kiran
Key DBA
Key DBA
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1723 Visits: 655
Kiran,

Thanks for posting that everything is working. See you around the Forums.

"Key"
MCITP: DBA, MCSE, MCTS: SQL 2005, OCP
msraghunathan
msraghunathan
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 23
hi,

The decode functionality in oracle is something similar to
"if then else...". But what you have stated is "select case...end"
let me clearly explain you a scinario,
table student contains name and total marks
i can say "pass" or "fail" by give a query like
"select name, decode(total > 250, "pass","fail") status from student"
how will u achieve the same scinario in sql-server.

regds
rishi
vetrivels
vetrivels
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 8
Rishi,

You can do the same with 'select case...end'

SELECT
CASE
WHEN total > 250 THEN 'pass'
ELSE 'fail'
END as 'status'
FROM student


-Vetri



msraghunathan
msraghunathan
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 23
Dear vetri,

What you have provided was sounds fine..but actually im looking for any in-built function like how oracle provided the "DECODE"........
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search