Connecting Tech Pros Worldwide Forums | Help | Site Map

SELECT yesterdays records

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,156
#1: Jun 26 '09
I need to do a query something like
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM invoices WHERE date= 'yesterdays date'
It is not as simple as one might think which means ideas I have looked at vary widly.
Could someone please suggest a tidy looking way of doing this.
Not tested below version but there is a cleaner way surely
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM invoices 
  2. WHERE date>=(CAST( FLOOR( CAST( GETDATE() 
  3. AS FLOAT ) ) AS DATETIME ) -1) 
  4. AND date<=(CAST( FLOOR( CAST( GETDATE() 
  5. AS FLOAT ) ) AS DATETIME ))

ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,992
#2: Jun 26 '09

re: SELECT yesterdays records


Will this help?

Good luck!!

--- CK
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,156
#3: Jun 26 '09

re: SELECT yesterdays records


Will look ar this ck9663.
Probably going to use a generic query with
Expand|Select|Wrap|Line Numbers
  1. WHERE date BETWEEN '$start 00:00:00.000'
  2.         AND '$finish 23:59:59.999'
This being a php application, and pass the same date for yesterday
nbiswas's Avatar
Member
 
Join Date: May 2009
Location: India
Posts: 89
#4: Jun 28 '09

re: SELECT yesterdays records


Try this

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM invoices WHERE date=DATEADD(day,-1,getdate())
Hope this helps
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,156
#5: Jun 29 '09

re: SELECT yesterdays records


Unfortunately this can't work nbiswas because the time element is an integral part of sql server DATETIME.
You can get away with it if all DATETIME time elements are 00.00.0000, but in this case they are not.
Newbie
 
Join Date: Jan 2010
Posts: 1
#6: Jan 2 '10

re: SELECT yesterdays records


I used this:

SELECT CONVERT(varchar, DATEADD(day, -1, GETDATE()), 101) AS yDate, CONVERT(varchar, yourDateHere, 101) AS sDate
FROM yourTableHere
WHERE stuff meets your parameters...

This way both the dates we are comparing are in the same format "mm/dd/yyyy."

You can use CAST but this seems a bit cleaner to me.

Hope this helps someone!
Reply


Similar Microsoft SQL Server bytes