If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Equivalent of Oracle's DECODE function on SQLServer

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-02, 14:58
jofph jofph is offline
Registered User
 
Join Date: Mar 2002
Posts: 4
Equivalent of Oracle's DECODE function on SQLServer

What is the equivalent function in SQL server for the DECODE function in Oracle?

For example:
select decode(customer_type,'B','Business','Non Business')
from CUSTOMER_TABLE

This is the function statement that we use currently in Oracle. I need to do this same function in Sql server.

Thanks for all help.
Reply With Quote
  #2 (permalink)  
Old 03-04-02, 15:29
achorozy achorozy is offline
Registered User
 
Join Date: Dec 2001
Location: Toronto, Canada
Posts: 335
I'm assuming that customer_type may or may not contain the character 'B'.

For SQL Server

Code:
SELECT CASE customer_type
                 WHEN 'B' THEN 'Business'
                  ELSE 'Non Business'
            END
FROM CUSTOMER_TABLE 

OR

SELECT CASE WHEN customer_type = 'B' THEN 'Business'
                  ELSE 'Non Business'
            END
FROM CUSTOMER_TABLE
Reply With Quote
  #3 (permalink)  
Old 06-29-11, 16:09
bob popular bob popular is offline
Registered User
 
Join Date: Jun 2011
Posts: 1
Equivalent of Oracle's DECODE function on SQLServer

That's good to know. I was wondering the same thing since I've been back in Oracle dabbling with SQL again. I guess my question is whether there is SQL SRVR equivalent function to DECODE or if you have to implement the more verbose option. Any ideas?

Thanks in advance!
Reply With Quote
  #4 (permalink)  
Old 06-29-11, 16:45
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Create a function called dbo.decode with achorozy's solution if you feel typing the CASE statement takes too long.

Anyway, if you don't know how a CASE construct works, take the (very) short time it takes to learn it. It can be very handy.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On