Get all records from Last Two Days?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Get all records from Last Two Days?

    Hi Everyone,

    I use the following to get records from the last two days in MySql:
    where date_entered <= curdate() and date_entered >=
    DATE_SUB(curdat e(),INTERVAL 2 day)

    I'm looking to do the same in MS-Sql server but I'm just not getting it.
    I've got this so far which does not work:
    where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDAT E(),INTERVAL 2
    day)

    then I tried this:
    WHERE hit_date >= DATEDIFF(GETDAT E(), (GETDATE()-2)>

    Essentially, I need all records from the last two days.

    Any help or guidance in this matter would be greatly appreciated.

    -JohnyB


  • Tom Moreau

    #2
    Re: Get all records from Last Two Days?

    Try:

    WHERE hit_date >= GETDATE() - 2


    --
    Tom

    ----------------------------------------------------
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Toronto, ON Canada
    ..
    "John" <nothanks@nope. com> wrote in message
    news:7JKdnbE0F6 Nc-tjZRVn-uQ@adelphia.com ...
    Hi Everyone,

    I use the following to get records from the last two days in MySql:
    where date_entered <= curdate() and date_entered >=
    DATE_SUB(curdat e(),INTERVAL 2 day)

    I'm looking to do the same in MS-Sql server but I'm just not getting it.
    I've got this so far which does not work:
    where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDAT E(),INTERVAL 2
    day)

    then I tried this:
    WHERE hit_date >= DATEDIFF(GETDAT E(), (GETDATE()-2)>

    Essentially, I need all records from the last two days.

    Any help or guidance in this matter would be greatly appreciated.

    -JohnyB


    Comment

    • John

      #3
      Re: Get all records from Last Two Days?


      "Tom Moreau" <tom@dont.spam. me.cips.ca> wrote in message
      news:4Sd1g.5357 $wK1.227431@new s20.bellglobal. com...[color=blue]
      > Try:
      >
      > WHERE hit_date >= GETDATE() - 2
      >
      >
      > --
      > Tom[/color]

      Yes. That's it.

      Thanks!

      JB




      [color=blue]
      >
      > ----------------------------------------------------
      > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
      > SQL Server MVP
      > Toronto, ON Canada
      > .
      > "John" <nothanks@nope. com> wrote in message
      > news:7JKdnbE0F6 Nc-tjZRVn-uQ@adelphia.com ...
      > Hi Everyone,
      >
      > I use the following to get records from the last two days in MySql:
      > where date_entered <= curdate() and date_entered >=
      > DATE_SUB(curdat e(),INTERVAL 2 day)
      >
      > I'm looking to do the same in MS-Sql server but I'm just not getting it.
      > I've got this so far which does not work:
      > where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDAT E(),INTERVAL
      > 2
      > day)
      >
      > then I tried this:
      > WHERE hit_date >= DATEDIFF(GETDAT E(), (GETDATE()-2)>
      >
      > Essentially, I need all records from the last two days.
      >
      > Any help or guidance in this matter would be greatly appreciated.
      >
      > -JohnyB
      >
      >[/color]


      Comment

      • Jens

        #4
        Re: Get all records from Last Two Days?

        Use the dateadd (Or Datediff) for that:

        hit_date >= DATEADD(d,-2,GETDATE()), keep in mind that this will
        substract the days including the current time, so a 04/19/2006 1:09
        will result in 04/17/2006 1:09.

        HTH, Jens Suessmeyer.

        ---

        ---

        Comment

        • Erland Sommarskog

          #5
          Re: Get all records from Last Two Days?

          John (nothanks@nope. com) writes:[color=blue]
          > I use the following to get records from the last two days in MySql:
          > where date_entered <= curdate() and date_entered >=
          > DATE_SUB(curdat e(),INTERVAL 2 day)
          >
          > I'm looking to do the same in MS-Sql server but I'm just not getting it.
          > I've got this so far which does not work:
          > where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDAT E(),INTERVAL[/color]
          2[color=blue]
          > day)
          >
          > then I tried this:
          > WHERE hit_date >= DATEDIFF(GETDAT E(), (GETDATE()-2)>
          >
          > Essentially, I need all records from the last two days.[/color]

          Since you appear to be trying out syntax at random, I must ask: did
          you ever consider to consult Books Online?



          --
          Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

          Books Online for SQL Server 2005 at

          Books Online for SQL Server 2000 at

          Comment

          • Madhivanan

            #6
            Re: Get all records from Last Two Days?

            Do you mean yesterday and today's data only?

            Select * from yourtable
            where datecol>=Datead d(day,datediff( day,o,getdate() ),-1)

            Madhivanan

            Comment

            Working...