Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DECODE( ) function in SQL Server

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 a Oracle query; how can I use DECODE() function in SQL Server 2005?

like image 858
Domnic Avatar asked Oct 13 '09 09:10

Domnic


People also ask

What is the decode function in SQL Server?

DECODE compares the expression to each search value one by one. If expression is equal to a search, then the corresponding result is returned by the Oracle Database. If a match is not found, then default is returned. If default is omitted, then Oracle returns null.

Is there a decode in SQL Server?

DECODE function is used to perform procedural IF-THEN-ELSE logic in SQL. The function is a close relative of CASE statements. It is a built-in function in ORACLE / PL SQL database management servers.

What is the equivalent of Decode in SQL Server?

In SQL Server the equivalent code is CASE statement. Here are the examples regarding how DECODE can be written in SQL Server.

How do I decode a value in SQL query?

The DECODE function returns a value that is the same datatype as the first result in the list. If the first result is NULL, then the return value is converted to VARCHAR2. If the first result has a datatype of CHAR, then the return value is converted to VARCHAR2. If no matches are found, the default value is returned.


2 Answers

You could use the 'CASE .. WHEN .. THEN .. ELSE .. END' syntax in SQL.

like image 113
daxsorbito Avatar answered Nov 08 '22 08:11

daxsorbito


If I understand the question correctly, you want the equivalent of decode but in T-SQL

Select YourFieldAliasName = CASE PC_SL_LDGR_CODE     WHEN '02' THEN 'DR'     ELSE 'CR' END 
like image 24
Andrew Avatar answered Nov 08 '22 06:11

Andrew