Solved

Unable to retrieve CURDATE() from table.

Posted on 2008-10-27
10
435 Views
Last Modified: 2012-05-05
Hi,

I have a table which contains a datetime column. When I run a query to bring back only records with todays date it retrieves nothing.

My table is called: smsin    - and the field is called: validDate

This field is in datetime format in the database.

The SQL code I'm running is : SELECT * FROM smsin WHERE validDate = CURDATE()
 
0
Comment
Question by:kenuk110
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22812775
this should work better:

SELECT * FROM smsin WHERE validDate >= CURDATE() AND validDate <= DATEADD(CURDATE(), INTERVAL 1 DAY)

Open in new window

0
 
LVL 2

Expert Comment

by:barfi
ID: 22812876
Can you also try following and see if you get the desired results:

 SELECT * FROM smsin WHERE validDate = sysdate
 
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 22812959
A datetime column contains a date AND a time. The curdate() function returns only a date. That's why they won't match.

The suggestion from angelIII tests if the validDate column is between (including) todays date and tomorrows date. However, if your validDate columns has a timestamp="00:00:00", this suggestion would also return rows with validDate tomorrow. I think this is a better solution:
SELECT * FROM smsin WHERE validDate BETWEEN
  CONCAT(CURDATE(),' 00:00:00') AND 
  CONCAT(CURDATE(),' 23:59:59')
 
# This is a modified version of angelIII's suggestion:
SELECT * FROM smsin WHERE 
  validDate >= CURDATE() AND 
  validDate < DATEADD(CURDATE(), INTERVAL 1 DAY)

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22812979
barfi,

the sysdate() function returns a full timestamp, including time. Only date is wanted in this case.
0
 

Author Comment

by:kenuk110
ID: 22813024
Hi,

All I get is syntax error when I try to run the scripts provided? I'm using MySQL 5.1 and using Navicat to run the script from the console, not sure if this makes a difference?
0
 

Author Closing Comment

by:kenuk110
ID: 31510309
Hi,

The first solution you provided works a treat.

Thanks.

Ken
0
 
LVL 1

Expert Comment

by:fReAkCoRp
ID: 22813049
You can use the DAY,MONTH and YEAR Functions


or using  sql functions like verifing the current day in which day of the year is and validating to the one you have in the database.

here are some more date and time functions for sql
http://msdn.microsoft.com/en-us/library/ms186724.aspx#GetDateandTimeParts

hope this helps, regards!


select * from test where DAY(validDate)=DAY(GETDATE()) AND MONTH(validDate)=MONTH(GETDATE()) and YEAR(validDate)=YEAR(GETDATE())
 
or try this
 
select * from test where DATEPART(dayofyear,validDate)=DATEPART(dayofyear,GETDATE())

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22813066
Please show the scripts/statements you are trying to run and the corresponding error message. It's hard for us to guess what is going wrong...
0
 
LVL 1

Expert Comment

by:fReAkCoRp
ID: 22813087
Try this in MySQL

select * from issues where DAYOFYEAR(hrdate)=DAYOFYEAR(now())

there not the same functions in sql or mysql
0
 
LVL 1

Expert Comment

by:fReAkCoRp
ID: 22813102
Here are some more info on Date and Time functions in MySQL
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question