Link to home
Start Free TrialLog in
Avatar of vbnetcoder
vbnetcoder

asked on

compare dates

I have a query like this

Select * from Table
where datecreated = CURRENT_TIMESTAMP

What is the best way to update this query so it only looks at date and not the time?
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America 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
Avatar of xiao jinshou
xiao jinshou

Select * from Table
 where datecreated = DATEPART ( d , getdate() )
select * from Table where cast(DateInserted as Date) = cast(Current_TimeStamp as Date)

Open in new window

The "best way" is to store the date information without the time. Any functions you execute in the WHERE clause will slow things down due to function call overhead and/or lack of indexing.

You might want to consider adding a computed column to your table and then indexing on that column. If you're doing these sorts of queries frequently or on large sets of data you'll find performance will be greatly improved using this approach.
The best way is to avoid using any function on a table column; otherwise you make the condition non-sargable.  Functions on literals/static values don't matter.
If the datecreated column is a date only data type then you don't need to do nothing as it will truncate the time and will store only the date part.
Avatar of vbnetcoder

ASKER

ty