Getting specific dates in SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bellgodz
    New Member
    • Jun 2008
    • 3

    Getting specific dates in SQL

    I've been looking at some code posted here and have been trying to dissect all the date functions to see what they return but am still having trouble. I'm trying to get the date of the 1st thursday in a given month and if that date falss on either the 1st or 3rd, I need the date of the next thursday.

    Example:
    Month of May, the 1st thursday is 5/1/2008
    I need the next thursday, which is 5/8/2008.

    Thanks in advance,
    Brian
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    hi,
    I tried something which will give you all the dates comes on the give day.

    My logic is i have taken the current date into @date,
    2. Then taken @stardate as subtracted days from @date.
    3. taken @enddate by adding one month to the @startdate.
    4. takel the dates which has datename as given day between startdate and enddate.

    [code=sql]

    declare @date datetime,
    @day int

    declare @startdate datetime,
    @enddate datetime
    set @date = getdate()

    set @day = datepart(day,ge tDate())-1

    set @startdate = DATEADD(day,-@day,getDate())
    set @enddate = DATEADD(month,1 ,@startdate)

    SELECT Date
    FROM
    ( Select dateadd(day,num ber,@startdate) AS Date
    from master.dbo.spt_ values
    where master.dbo.spt_ values.type='p'
    AND dateadd(day,num ber,@startdate) <=(@enddate)
    ) AS T
    WHERE DATENAME(dw, Date) = 'Thursday'


    [/code]

    change the datename as 'Sunday', 'Monday' as per the requirement

    thanks

    Comment

    • bellgodz
      New Member
      • Jun 2008
      • 3

      #3
      Thanks, I think that may actually work. For my project I'm trying to automate the generation of a due date for bills. Since we pay our bills on thursdays I needed to get the next date for the next thursday. With a little modification I can use this to check for the next thursday if I pass it a date.

      Thanks again.

      Originally posted by deepuv04
      hi,
      I tried something which will give you all the dates comes on the give day.

      My logic is i have taken the current date into @date,
      2. Then taken @stardate as subtracted days from @date.
      3. taken @enddate by adding one month to the @startdate.
      4. takel the dates which has datename as given day between startdate and enddate.

      [code=sql]

      declare @date datetime,
      @day int

      declare @startdate datetime,
      @enddate datetime
      set @date = getdate()

      set @day = datepart(day,ge tDate())-1

      set @startdate = DATEADD(day,-@day,getDate())
      set @enddate = DATEADD(month,1 ,@startdate)

      SELECT Date
      FROM
      ( Select dateadd(day,num ber,@startdate) AS Date
      from master.dbo.spt_ values
      where master.dbo.spt_ values.type='p'
      AND dateadd(day,num ber,@startdate) <=(@enddate)
      ) AS T
      WHERE DATENAME(dw, Date) = 'Thursday'


      [/code]

      change the datename as 'Sunday', 'Monday' as per the requirement

      thanks

      Comment

      Working...