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?
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.
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.
In SQL Server the equivalent code is CASE statement. Here are the examples regarding how DECODE can be written in SQL Server.
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.
You could use the 'CASE .. WHEN .. THEN .. ELSE .. END' syntax in SQL.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With