Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Compare dates

Author  Topic 

ranalk
Starting Member

49 Posts

Posted - 2012-01-15 : 11:44:54
Hi,

I have a log table that has datetime column (e.g. '2012-01-01 08:00:00).
I would like to retrive just the records of "yesterday"
What is the best way to compare between a getutcdate()-1 and the column that I have and to get only the records for yesterday.

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-15 : 11:55:10
just use where condition like

...
where datefield >= dateadd(dd,datediff(dd,0,getutcdate()),-1)
and datefield < dateadd(dd,datediff(dd,0,getutcdate()),0)
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ranalk
Starting Member

49 Posts

Posted - 2012-01-16 : 04:35:46
quote:
Originally posted by visakh16

just use where condition like

...
where datefield >= dateadd(dd,datediff(dd,0,getutcdate()),-1)
and datefield < dateadd(dd,datediff(dd,0,getutcdate()),0)
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Works good ! Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-16 : 05:29:56
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -