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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select * from Table where cast(DateInserted as Date) = cast(Current_TimeStamp as Date)
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.
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.
ASKER
ty
where datecreated = DATEPART ( d , getdate() )