Search code examples
sqlsql-servert-sqlcase-when

Overdue by -10 which means overdue by 10 minutes. Swap the '-10' to overdue by 10 minutes


CASE WHEN Color LIKE '%RED%' THEN (10 - (DATEDIFF(MINUTE,Calltime,GETDATE()))) 

Sometimes might give a -10 value. My question is when I get a value <0, how can I change that value to 10 minutes overdue.


Solution

  • You can nest CASE statements, but since you're now introducing text to the integer value, you'll have to cast all output to string:

    CASE WHEN Color LIKE '%RED%' THEN
        CASE WHEN (10 - (DATEDIFF(MINUTE,Calltime,GETDATE()))) >= 0 THEN CAST((10 - (DATEDIFF(MINUTE,Calltime,GETDATE()))) AS VARCHAR(MAX))
        ELSE CAST(ABS((10 - (DATEDIFF(MINUTE,Calltime,GETDATE())))) AS VARCHAR(MAX)) + ' minutes overdue'
        END
    ELSE -- Additional SQL for when color is not red
    END