Solved

Unable to retrieve CURDATE() from table.

Posted on 2008-10-27
10
434 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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.​
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…

696 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