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
 General SQL Server Forums
 New to SQL Server Programming
 Help with Previous Date

Author  Topic 

misnole
Starting Member

5 Posts

Posted - 2012-04-04 : 14:56:11
I'm trying to write a SQL statement where I pull all records for the previous day. My initial attempt looks like:
select dateadd(day, -1, CONVERT(DATETIME, GETDATE(), 102))

which returns:
2012-04-03 14:51:14.317


So I can tell the code is actually returning yesterday's date at the exact time I test run the query.

But when I insert the code into my larger SQL statement to pull actual data, nothing is returned. I believe because it might be that it is not only looking for records for that previous day, but for records that match that hour/minute as well.

Would this be in the case? And if so, how could I strip the date down into the yy/mm/dd only?

Thanks,

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-04 : 15:01:31
DateAdd(day, 0, DateDiff(day, 0, getdate()-1))
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-04 : 18:15:10
see this to understand similar logic

http://visakhm.blogspot.com/2010/01/some-quick-tips-for-date-formating.html

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

Go to Top of Page

misnole
Starting Member

5 Posts

Posted - 2012-04-05 : 10:33:09
Thanks russell, that worked.
I'll check that out visakh16.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-05 : 12:18:39
Welcome
Go to Top of Page
   

- Advertisement -