?
Solved

Unable to retrieve CURDATE() from table.

Posted on 2008-10-27
10
Medium Priority
?
436 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 1000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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