Skip to content
Advertisement

SQL CASE WHEN (1=1) What is the 1=1 doing?

I have a SQL statement that I did not write and I am trying to get my head around it.

CASE
    WHEN (1 = 1) THEN A.AMT
    WHEN (A.ACC_NO = '1') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '2') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '3') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '4') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '5') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '6') THEN A.AMT - 100000000
    ELSE A.FACE_AMT
END AS TRUE_AMT,    

Can someone please explain what the (1=1) is doing please?

From researching this online my interpretation is that 1=1 means always true, so this would mean the case statement would always return the amt field?

However this doesn’t seem to make sense as this would mean none of the other conditions would ever be met?

Advertisement

Answer

It is placeholder for always true(typical usage is for debug/testing):

CASE
    WHEN (1 = 1) THEN A.AMT
    WHEN (A.ACC_NO = '1') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '2') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '3') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '4') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '5') THEN A.AMT - 100000000
    WHEN (A.ACC_NO = '6') THEN A.AMT - 100000000
    ELSE A.FACE_AMT
END AS TRUE_AMT,  


-- entire case is the same as:
A.AMT AS TRUE_AMT
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement