SQL day of week question

  • I need to write a query to get the prior Sunday of the current week I'm in. Is there a built in function in SQL or will I need to write one?

    i.e. Today we are in week 17 and the prior Sunday is 4/18/2004

    Thanks!

  • If Sunday is defined as the start of the week, something like this works:

    SELECT DATEADD(dd, -1 * (select datepart(dw, GETDATE()) -1), GETDATE())

    You can use CONVERT() to get it into the format you want. This is, of course, assuming if the day is Sunday you want that day, not a week prior.

    K. Brian Kelley
    @kbriankelley

  • Thanks Brian,

    That worked very nicely. I used the field name (which was a date) in place of the getdate() within a select while building a view.

    Thanks again!

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

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