If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-22-09, 12:15
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
Dates

I'm missing something her in the formatting of datetime. This query does not fail it just does not return rows which I know exist.

SELECT *
FROM patients
WHERE timestamp BETWEEN 2009-11-15 AND 2009-12-16

thanks
Reply With Quote
  #2 (permalink)  
Old 12-22-09, 14:57
scooby_at_work scooby_at_work is offline
Registered User
 
Join Date: Sep 2009
Posts: 44
Two problems:

One: It's a bad idea to name a column "timestamp" because that's a reserved word. If you have to use it, quote your identifier.

Two: MySQL is reading 2009-11-15 as 2009 minus 11 minus 15, or 1983. So your query is really saying:

Code:
SELECT * FROM patients
WHERE timestamp between 1983 AND 1981
MySQL won't warn you when you make a mistake like this. The correct syntax for a date literal, assuming you're using 5.5.

Your code will probably want, depending on what date type you're actually using:

Code:
SELECT *
FROM patients
WHERE `TIMESTAMP` BETWEEN date '2009-11-15' AND date '2009-12-16'
Reply With Quote
  #3 (permalink)  
Old 12-22-09, 18:28
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
Dates

Just kick me, your right, the quotes make all the difference!

Thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On