Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (6.1k points)
SELECT PC_COMP_CODE,
       'R',
       PC_RESUB_REF,
       DECODE(PC_SL_LDGR_CODE, '02', 'DR', 'CR'),
       PC_DEPT_NO DEPT,
       '', --PC_DEPT_NO,
       PC_SL_LDGR_CODE + '/' + PC_SL_ACNO,
       SUM(DECODE(PC_SL_LDGR_CODE, '02', 1, -1) * PC_AMOUNT),
       PC_CHEQUE_NO CHQNO
  FROM GLAS_PDC_CHEQUES
 WHERE PC_RESUB_REF IS NOT NULL 
   AND PC_DISCD NOT IN ('d', 'D', 'T') 
GROUP BY PC_RESUB_REF, 
         PC_COMP_CODE, 
         'JJ', 
         PC_SL_LDGR_CODE + '/' + PC_SL_ACNO, 
         PC_DEPT_NO, 
         PC_CHEQUE_NO, 
         DECODE(PC_SL_LDGR_CODE, '02', 'DR', 'CR')

Above is an oracle query; how can I use the DECODE() function in the SQL Server 2005?

1 Answer

0 votes
by (12.7k points)
edited by

In my case I had used it in a lot of places the first example if you have 2 values for a select statement like gender (Male or Female) then use the following statement:

SELECT CASE Gender WHEN 'Male' THEN 1 ELSE 2 END AS Gender

If there is more numerous than one condition alike nationalities you could also use it as the below statement:

SELECT CASE Nationality 
WHEN 'AMERICAN'   THEN 1 
WHEN 'BRITISH'   THEN 2
WHEN 'GERMAN'    THEN 3 
WHEN 'EGYPT'     THEN 4 
WHEN 'PALESTINE' THEN 5 
ELSE 6 END AS Nationality 

If you want to learn more about SQL, Check out this SQL Certification by Intellipaat. 

For more information visit :

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 23, 2021 in SQL by dev_sk2311 (44.4k points)
0 votes
1 answer
asked Dec 17, 2020 in SQL by Appu (6.1k points)
0 votes
0 answers
asked Dec 29, 2020 in SQL by biscottob (120 points)

Browse Categories

...