locked
DATEADD RRS feed

  • Question

  • Hello,

    i want to find last month date using dateadd function.

    where logindate > 12/1/2008 and logindata <12/31/2008


    12/1/2008 = dateadd(m,-1,getdate()) ?

    12/31/2008 = dateadd(m,-1,getdate()) ?


    same way, i want to find last week date using dateadd function. How?

    where logindate > 12/22/2008 and logindata <12/28/2008 ?
    Friday, January 2, 2009 4:56 PM

Answers

  •  Assuming that you're running Sunday - Saturday (as SQL Server is), this would do the trick:

    SELECT

    DATEADD(day, -6 - DATEPART(dw, GetDate()), GETDATE()) AS FirstDayOfLastWeek,

    DATEADD(day, - DATEPART(dw, GetDate()), GETDATE()) AS LastDayOfLastWeek

    HTH.....


    EDIT: You would still need to convert it to a varchar, if you need to strip off the time portion.


    Aaron Alton | thehobt.blogspot.com
    Friday, January 2, 2009 5:06 PM
  •  

     

    ....  
    where logindate >=DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)  
     and logindata < DATEADD(mm, DATEDIFF(m,0,GETDATE()),0) 

     

    Friday, January 2, 2009 5:08 PM

All replies

  •  Assuming that you're running Sunday - Saturday (as SQL Server is), this would do the trick:

    SELECT

    DATEADD(day, -6 - DATEPART(dw, GetDate()), GETDATE()) AS FirstDayOfLastWeek,

    DATEADD(day, - DATEPART(dw, GetDate()), GETDATE()) AS LastDayOfLastWeek

    HTH.....


    EDIT: You would still need to convert it to a varchar, if you need to strip off the time portion.


    Aaron Alton | thehobt.blogspot.com
    Friday, January 2, 2009 5:06 PM
  •  

     

    ....  
    where logindate >=DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)  
     and logindata < DATEADD(mm, DATEDIFF(m,0,GETDATE()),0) 

     

    Friday, January 2, 2009 5:08 PM