How to select yesterdays records in sql server 2008?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    How to select yesterdays records in sql server 2008?

    hi i am writing the query to select all records for yesterday here is what i have
    [code=sql]
    select *
    from ews_tiff.dbo.wh atever_merged
    where datepart(yy,tim e_stamp) = datepart(yy,dat eadd(m,0,getdat e()))
    and datepart(m,time _stamp) = datepart(m,date add(m,0,getdate ()))
    and DATEPART(D,time _stamp)=datepar t(D,DATEADD(D,-1,GETDATE()))
    [/code]
    the column time_stamp is of type DATE
    but when i run this query in the query analyser window it goes on debugging
    any ideas , i am new to sql server
    regards,

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

    #2
    I wouldn't do it that way.
    On the first of every month it will fail because the months do not match
    Code:
    datepart(m,time_stamp) = datepart(m,dateadd(m,0,getdate()))
    Another disaster looms on January 1.
    Is it between midnight and midnight the previous day you require?
    I might have something using BETWEEN, CAST and CONVERT

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      So what do you think i have'nt worked with SQL SERVER that much, if it would have been with mysql it would not be that much annoying for me ,
      should i be achieving this task by targeting the last 24 hours by tracking the time
      regards,
      omer aslam

      Comment

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

        #4
        So what do you think i have'nt worked with SQL SERVER that much, if it would have been with mysql it would not be that much annoying for me
        I have no idea what you are talking about here. Why would MySQL be any easier?
        should i be achieving this task by targeting the last 24 hours by tracking the time
        I am confused, it is your task, which time period do you want?
        Is it 'yesterday' as in 00.00.01 - 23.59.59 or the last 24 hours?

        Comment

        • Jacques Franken
          New Member
          • Dec 2010
          • 2

          #5
          How about :

          SELECT *
          FROM from ews_tiff.dbo.wh atever_merged
          WHERE time_stamp = DATE(Now)-1

          If all you need are all records dated yesterday the this should do it : DATE(Now)= today -1 = yesterday, right ?

          Comment

          Working...