Chris Tolworthy
Chris Tolworthy

Reputation: 2096

How to tell if an SQL date is still in the future (not a query, I already have the desired row)

I have a row from an SQL table, and one field is a DATE. (The value may be undefined, as DATE has no default value). How can I tell if this date, if it exists, is still in the future?

I searched for answers and found lots of ways to extract all records that pass this test, but I don't want to do that. I just want to check this one previously extracted record. Sorry for the newbie question!

$userQuery  = "SELECT * FROM  `passwords` WHERE `name` = '$name' LIMIT 1";
$userResult     = mysql_query($userQuery);    
$userRow        = mysql_fetch_assoc($userResult); 
$bestBeforeDate = $userRow['bestBeforeDate']; // field is in DATE format
// what now? How to find if the 'best Before Date' has passed?

Upvotes: 1

Views: 3424

Answers (1)

juergen d
juergen d

Reputation: 204874

You can do it in SQL as additional column

select case when (current_timestamp < date_column) 
            then 1
            else 0
            end as is_in_future
from your_table

EDIT

You can do this in one query while reading other values from your table.
Example (not knowing the column names of your table)

select id,
       date_column,
       other_column,
       case when (current_timestamp < date_column) 
            then 1
            else 0
       end as is_in_future
from your_table
where some_conditions

Example output:

id  date_column   other_column   is_in_future
 1  2012-01-01    abc            0
 2  2012-08-01    def            1
 ...

Upvotes: 5

Related Questions