Answers for "How to group by week (7 days) in SQL Server"

SQL
0

How to group by week (7 days) in SQL Server

SELECT
    CONVERT(VARCHAR(5), DATEADD(WK, WK, 0), 103) + ' - ' + CONVERT(VARCHAR(5), DATEADD(DD, 6, DATEADD(WK, WK, 0)), 103)
    , Cnt
FROM (
    SELECT
        DATEDIFF(WK, 0, MyDate) AS WK
        , COUNT(*) AS Cnt
    FROM (VALUES
    ('2013-01-01')
    , ('2013-01-02')
    , ('2013-01-03')
    , ('2013-01-04')
    , ('2013-01-05')
    , ('2013-01-06')
    , ('2013-01-07')
    ) AS X(MyDate)
    GROUP BY DATEDIFF(WK, 0, MyDate)
) AS X
Posted by: Guest on May-05-2022

Code answers related to "SQL"

Browse Popular Code Answers by Language