convert a date into the date of the friday in the present week, ie the week ending

  • Hello

    I am currently using ONVERT(VARCHAR(10), dt_open_Date, 102) as Short_Date to just return the date, this gives me the below data;

    2004.03.04

    2004.03.08

    2004.03.09

    2004.03.10

    2004.03.11

    2004.03.15

    2004.03.16

    2004.03.17

    however i require the week ending date (ie the date of the friday for each week) so i would like the above date colum to show;

    2004.03.05

    2004.03.12

    2004.03.12

    2004.03.12

    2004.03.12

    2004.03.19

    2004.03.19

    2004.03.19

    can anyone offer me any help of how to acheive this?

    many thanks

  • Here's one way to do it. Adjust datefirst and/or the offset in dateadd() functions accordingly for what start-end days you want.

    -- The example shows how to find out on which dates a week

    -- starts and ends, given an arbitrary date.

    -- Start of weeks are on Mondays, and ends on Fridays

    set datefirst 1 -- Make sure week starts on a monday

    declare @today datetime

    set @today = '2004-03-04'

    select @today as 'today',

     dateadd(day, 1 - datepart(weekday, @today), @today) as 'first_weekday',

     dateadd(day, 5 - datepart(weekday, @today), @today) as 'last_weekday' -- friday

    today                    first_weekday            last_weekday           

    ------------------------ ------------------------ ------------------------

    2004-03-04 00:00:00.000  2004-03-01 00:00:00.000  2004-03-05 00:00:00.000

    /Kenneth

  • -- Friday

    DECLARE @Date DATETIME

    SET     @Date = GETDATE()

    SELECT CONVERT(VARCHAR(10), DATEADD(DAY, 6 - DATEPART(WEEKDAY, @Date), @Date), 101)



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply