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)
 check if the date was yesterday

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2011-08-03 : 15:27:05
I need to do a select to find if the records were added yesterday. There is a dateTime field???

Dave
Helixpoint Web Development
http://www.helixpoint.com

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-03 : 15:34:24
select ...
from t
where d >= DATEADD(dd, DATEDIFF(dd, 0, getdate()-1), 0) and d < DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2011-08-03 : 15:37:55
Dang you guys are fast. Tanx

Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-03 : 15:39:01
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-08-03 : 15:40:40
I advice to stop doing arithmetic operations on date and time values.
They are no longer supported in Denali.

Rewriting Tara's suggestion would then look like

1)
select ...
from t
where d >= DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0) and d < DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

2)
select ...
from t
where cast(d as date) = cast(getdate() as date)


Also see http://www.sqltopia.com/?page_id=35



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-08-08 : 09:58:22
<<
They are no longer supported in Denali.
>>

If the datatype is DATETIME, then no issues

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -