Solved

Unable to retrieve CURDATE() from table.

Posted on 2008-10-27
10
433 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 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Creating and Managing Databases with phpMyAdmin in cPanel.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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