Solved

Unable to retrieve CURDATE() from table.

Posted on 2008-10-27
10
426 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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
barfi,

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

Author Comment

by:kenuk110
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:kenuk110
Comment Utility
Hi,

The first solution you provided works a treat.

Thanks.

Ken
0
 
LVL 1

Expert Comment

by:fReAkCoRp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now