Week part

Posted on 2007-08-07
Last Modified: 2013-11-05
I have a sql query that groups on "Datepart(dw, DateField)" however in some cases the week should start of a Thrusday and end on the following wednesday. What is the best way to handle a week not starting on a Sunday?

Question by:craigdev
    LVL 6

    Expert Comment

    write a scaler function that returns the week number you want and use that in your select and group by.  use a case or if statement in the function to adjust the date based on start of week day.  Let me know if you'd like help with a function to do that.
    LVL 6

    Accepted Solution

    actually you can do it inline
    Datepart(dw, DATEADD(d,3,DateField) )-- change 3 to whatever number of days past or before sunday you need

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now