V4n1ll4
V4n1ll4

Reputation: 6099

MySQL select extra column if date is in future

I need to check whether my date field is in the future using MySQL and if so, add an extra column. It's important that I do not want to make this part of my where clause as I want to return rows where the date field is not in the future.

For example:

select 
    dateIsInFuture = 'true' if date > now()

I know the above is not valid syntax but I need to know how to write that in a valid SQL statement.

Thanks

Upvotes: 0

Views: 304

Answers (2)

Miguel Mesquita Alfaiate
Miguel Mesquita Alfaiate

Reputation: 2968

You can use if, but using this syntax:

SELECT IF(date > now(), 'true', 'false') as FutureFlag FROM YOUR_TABLE

Upvotes: 0

juergen d
juergen d

Reputation: 204756

select *, case when date_col > now() 
               then 'true'
               else 'false'
          end as dateIsInFututre
from your_table

Upvotes: 4

Related Questions