CASE with variable and static response

386 Views Asked by At

I am trying to form a CASE statement that will generate a fixed response if the criteria is met or will respond with the value in the field. I am getting a "800: Corresponding data types must be compatible in CASE expression or DECODE function." error when executing the query. From my understanding it is caused when the response types do not match, a mismatch in the format. However the response of f.frequency is a 9 digit integer if not null (so the type of f.frequency is INTEGER).

This works:

CASE
    WHEN f.frequency is null AND d.appid = 7 THEN '999000000'
    ELSE null
    END AS frequency,

Or this

CASE
    WHEN f.frequency is not null THEN f.frequency
    ELSE null
    END AS frequency,

but not when I combine them as in

CASE
    WHEN f.frequency is null AND d.appid = 7 THEN '999000000'
    WHEN f.frequency is not null THEN f.frequency
    ELSE null
    END AS frequency,

or like this

CASE
    WHEN f.frequency is null AND d.appid = 7 THEN '999000000'
    ELSE f.frequency
    END AS frequency,
1

There are 1 best solutions below

0
On

What I understand from the error is that '999000000' and f.frequency are different data types.

Casting '999000000' to whatever datatype your frequency column is should do the trick.

Also (at least in postgres) '999000000' is not an int so you may just want to remove the quotes