Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Query for How to compare today date and date field in table comparision is '='.
Posted
Updated 26-Jun-11 21:51pm
v2
Comments
OriginalGriff 27-Jun-11 3:51am    
Depends on what you are trying to do - are we talking about a WHERE clause? If so, what data are you comparing? Or are we talking about find all records with a date since midnight?
yarakiran 27-Jun-11 3:54am    
means i am searching records from table where demodate = 'todaydate'

The safest way is to use DatePart:
WHERE DATEPART(year, demodate)=DATEPART(year, GETDATE()) AND DATEPART(dy, demodate)=DATEPART(dy, GETDATE())
This returns only records where the year and day of year are the same - be aware that if you should read GETDATE once, and store it, as otherwise is may cause intermittent errors around new year!
 
Share this answer
 
Comments
yarakiran 27-Jun-11 4:20am    
thanks its working..
OriginalGriff 27-Jun-11 4:27am    
You're welcome!
Try this for SQL server,

SQL
SELECT yourField FROM yourTable WHERE yourDateField = GETDATE();


or for MySql

SQL
SELECT yourField FROM yourTable WHERE yourDateField = NOW();


Hope this helps
 
Share this answer
 
Comments
OriginalGriff 27-Jun-11 4:27am    
The only problem with that is that if the field is DateTime rather than just Date, it doesn't work...:laugh:
Wayne Gaylard 27-Jun-11 4:38am    
The OP specified a Date field. Your answer would be more fail safe.
If you are using SQL2008, you can convert to the Date datatype
For example in the AdventureWorks Database
SQL
SELECT  *
FROM    Sales.SalesOrderHeader
WHERE   CAST(OrderDate AS DATE) = CAST(GETDATE() AS DATE)

J
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900