SELECT yesterdays records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    SELECT yesterdays records

    I need to do a query something like
    Code:
    SELECT * FROM invoices WHERE date= [I]'yesterdays date'[/I]
    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
    Code:
    SELECT * FROM invoices 
    WHERE date>=(CAST( FLOOR( CAST( GETDATE() 
    AS FLOAT ) ) AS DATETIME ) -1) 
    AND date<=(CAST( FLOOR( CAST( GETDATE() 
    AS FLOAT ) ) AS DATETIME ))
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Will this help?

    Good luck!!

    --- CK

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      Will look ar this ck9663.
      Probably going to use a generic query with
      Code:
      WHERE date BETWEEN '$start 00:00:00.000'
      		AND '$finish 23:59:59.999'
      This being a php application, and pass the same date for yesterday

      Comment

      • nbiswas
        New Member
        • May 2009
        • 149

        #4
        Yesterday's Date

        Try this

        Code:
        SELECT * FROM invoices WHERE date=DATEADD(day,-1,getdate())
        Hope this helps

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          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.

          Comment

          • mjames727
            New Member
            • Jan 2010
            • 1

            #6
            Converting the dates first in the Select Statement

            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!

            Comment

            Working...