Link to home
Start Free TrialLog in
Avatar of kenuk110
kenuk110

asked on

Unable to retrieve CURDATE() from table.

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()
 
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this should work better:

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

Open in new window

Can you also try following and see if you get the desired results:

 SELECT * FROM smsin WHERE validDate = sysdate
 
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
barfi,

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

ASKER

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?
Hi,

The first solution you provided works a treat.

Thanks.

Ken
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

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...
Try this in MySQL

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

there not the same functions in sql or mysql
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