Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2006
    Posts
    12

    Unanswered: DateAdd function question

    Hi there,
    I want to know the last day of the previous month. i have this sentence to select the first day of the month maybe it can help.

    select dateadd(day, -(datepart(dd, getdate()))+1 , getdate())

    Regards
    Alejandro Mejia

  2. #2
    Join Date
    Mar 2006
    Posts
    12
    Making a deep search i solved this:

    select dateadd(day, -(datepart(dd, getdate())) , dateadd(month,1,getdate()))

  3. #3
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1

    last day of the previous month

    select dateadd(dd,-datepart(dd,getdate()),getdate())

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1

    Last day of a month

    Quote Originally Posted by amejiae
    select dateadd(day, -(datepart(dd, getdate())) , dateadd(month,1,getdate()))
    Your code does not work
    Code:
    declare @d1 datetime, @d2 datetime
    select @d1='2006-01-30', @d2='2006-03-31' 
    select dateadd(day, -(datepart(dd, @d1)) , dateadd(month,1,@d1))
          ,dateadd(day, -(datepart(dd, @d2)) , dateadd(month,1,@d2))
    
    -                              -                              
    Jan 29 2006 12:00AM            Mar 30 2006 12:00AM
    Modified
    Code:
    select dateadd(dd,-datepart(dd,dateadd(mm,1,@d1)),dateadd(mm,1,@d1))
          ,dateadd(dd,-datepart(dd,dateadd(mm,1,@d2)),dateadd(mm,1,@d2))
                                                                  
    -                              -                              
    Jan 31 2006 12:00AM            Mar 31 2006 12:00AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •