RomainD
RomainD

Reputation: 87

CASE statement in DateDiff (Amazon Redshift)

I am trying to implement a CASE statement in a datediff function but it throws an ERROR: syntax error at or near "case"

Even when using a simplified query like the following, the error arises:

select datediff(CASE WHEN 1=1 THEN 'month' ELSE 'month' end,'2009-01-01','2009-12-31') as nummonths;

Is it not possible to include CASE statement in the function?

Upvotes: 0

Views: 844

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

The first argument to datediff() in Redshift is a keyword. So, you cannot do what you want that simply.

Instead:

select (case when 1=1
             then datediff(month, '2009-01-01', '2009-12-31')
             else datediff(month, '2009-01-01', '2009-12-31')
        end) as nummonths;

I assume this is oversimplified code, because obviously the case is not needed in any version of this query.

Upvotes: 2

Related Questions