Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DECODE equivalent in SQL Server? 3

Status
Not open for further replies.

k108

Programmer
Jul 20, 2005
230
US
On Oracle there is a handy function called DECODE, for replacing a code with an actual word.

For example:

SELECT supplier_name,
decode(supplier_id, 10000, 'IBM',
10001, 'Microsoft',
10002, 'Hewlett Packard',
'Gateway') result
FROM suppliers;

Does SQL SERVER have an equivalent function?

Thanks!
 
I don't know how the decode function works, but this may do the trick.

Select Supplier_Name,
Case SupplierId When 10000 Then 'IBM'
When 10001 Then 'Microsoft'
When 10002 Then 'Hewlett Packard'
Else 'Gateway'
End As Result
From Suppliers

A better suggestion would be to create a lookup table in your database so you can 'simply' join the tables together.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
SELECT supplier_name
CASE supplier_id
WHEN 10000 THEN 'IBM'
WHEN 10001 THEN 'Microsoft'
WHEN 10002 THEN 'Hewlett Packard'
ELSE
'Gateway'
END
FROM suppliers
 
Experts have already suggested the usage of case.

On a friendly note, Case is powerful, elegant and simple than decode. One site to prove that. Not that it matters to you but, I have not used decode in over a year.

Regards,
AA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top