By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

Dates in MySQL queries

P: n/a
I am trying to do select all of the records in a table where they data
has not expired.

The table contains two fields Subject (varchar(100)) and ExpiryDate
(Date).

I am getting all of the records returned when I execute:

$sql = "select * from tbl where ExpiryDate > " . date("Y-m-d");
mysql_query($sql);

The expiry date in the record I am looking at is 2004-03-25 and the
query that is being executed is:

select * from tbl where ExpiryDate > 2004-03-28

What am I missing? Can anyone recommend a good site to look at that
discusses dates in MySQL queries?

Thanks in advance,
Mark
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 28 Mar 2004 16:54:52 +0100, Mark Stevens <re**@my.sig> wrote:
I am trying to do select all of the records in a table where they data
has not expired.

The table contains two fields Subject (varchar(100)) and ExpiryDate
(Date).

I am getting all of the records returned when I execute:

$sql = "select * from tbl where ExpiryDate > " . date("Y-m-d");
mysql_query($sql);

The expiry date in the record I am looking at is 2004-03-25 and the
query that is being executed is:

select * from tbl where ExpiryDate > 2004-03-28

What am I missing?
Quotes.

2004-03-28 is an arithmetic expression which equals 1978. This isn't a valid
date representation, so it either gets silently turned into the zero-date
0000-00-00, which is less than all the dates in your table, or they're both
turned into strings and compared, which still gives the same result. (I don't
know off the top of my head which approach MySQL takes in this case).

Whereas '2004-03-28' is a string representation of a date, which MySQL can use
to compare with a date field.
Can anyone recommend a good site to look at that
discusses dates in MySQL queries?


http://www.mysql.com/documentation/m...e_calculations
http://www.mysql.com/documentation/m...and_time_types
http://www.mysql.com/documentation/m...time_functions
http://www.mysql.com/documentation/m...tml#Using_DATE

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
On Sun, 28 Mar 2004 17:05:51 +0100, Andy Hassall <an**@andyh.co.uk> wrote:
2004-03-28 is an arithmetic expression which equals 1978.


1973, even. Bleh :-p

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3

P: n/a
On Sun, 28 Mar 2004 17:05:51 +0100, Andy Hassall <an**@andyh.co.uk>
wrote:
Quotes.

2004-03-28 is an arithmetic expression which equals 1978. This isn't a valid


Thanks, works fine now.

Regards,
Mark
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.