Question: I have the following query.

```
select case when <some condition> then max(modify_date) else NULL::DATETIME end from <table>

```

I want to return null in the else case but this throws a syntax error. How this can be resolved? I have tried `CAST(NULL AS DATETIME)` too.

Answer: 
You need to specify which DATETIME type. Assuming `modify_date` is a DATETIME YEAR TO DAY, then you need to write:

```
SELECT CASE
       WHEN <some condition>
       THEN MAX(modify_date)
       ELSE NULL::DATETIME YEAR TO DAY -- or CAST(NULL AS DATETIME YEAR TO DAY)
       END
  FROM <table>

```

If the `modify_date` is actually DATETIME YEAR TO SECOND or something similar, change the cast accordingly. However, you need `<t1> TO <t2>` after `DATETIME`.

You might still run into an issue with 'aggregate vs non-aggregate' in the CASE expression. You might be better off with:

```
SELECT MAX(CASE
           WHEN <some condition>
           THEN modify_date
           ELSE NULL::DATETIME YEAR TO DAY
           END)
  FROM <table>

```

Or there might be another way to deal with it. It depends on what you're expecting as output. The aggregate outputs one row for the entire table; the null is a 'row per row' (which is why I think there'll be trouble). You don't specify a GROUP BY clause.
Have you tried:

```
select (case when <some condition> then max(modify_date) end)
from <table>;

```

Not having the `else` clause automatically returns `NULL`.