开发者

How to take weekdays using sql query?

开发者 https://www.devze.com 2023-02-17 23:21 出处:网络
I have 2 days Date_from 01/03/2011 Date_to15/03/2011 How to take special week days (Monday, Tuesday and Friday ) from above two date range?

I have 2 days

Date_from 01/03/2011 
Date_to   15/03/2011

How to take special week days (Monday, Tuesday and Friday ) from above two date range?

I need output like below

Date_From   Date_To    
01/03/2011  01/03/2011  'Tuesday
04/03/2011  04/03/2011  'Friday
07/03/2011  08/03/2011  'Monday and Tuesday
11/03/2011  11/03/2011  'Friday
14/03/2011  15/03/2011 开发者_StackOverflow社区 'Monday and Tuesday


WITH    wd (d) AS
        (
        SELECT  0
        UNION ALL
        SELECT  d + 1
        FROM    wd
        WHERE   d < 6
        )
SELECT  d.date_from,
        (
        SELECT  DATENAME(w, DATEADD(d, d, date_from))  + ' ' AS [text()]
        FROM    wd
        WHERE   DATEPART(w, DATEADD(d, d, date_from)) IN (2, 5, 6)
                AND DATEADD(d, d, date_from) <= date_to
        FOR XML PATH('')
        )
FROM    dates d


If this is for SQL Server do this:

SELECT DATENAME(dw, GETDATE())


use WEEKDAY or DAYOFWEEK

0

精彩评论

暂无评论...
验证码 换一张
取 消