如何在SQL Server中显示日期名称?

时间:2019-04-26 11:17:04

标签: sql sql-server date sql-server-2017

我在SQL服务器上有一个查询

我想显示如下:

CDATE      | CDAY
2019-04-01 | Monday
2019-04-02 | Tuesday
...        | ......
2019-04-30 | Tuesday

但是我发现错误如下:

  

从字符转换日期和/或时间时转换失败   字符串。

请有人帮忙

DECLARE @V_DATE DATE = GETDATE()

;WITH CTE_DATE AS (
        SELECT  DATEADD(dd,-(DAY(@V_DATE)-1),@V_DATE) CDATE,
        DATENAME(dw, CONVERT(varchar, DATEADD(dd,-(DAY(@V_DATE)-1),@V_DATE))) CDAY
        UNION ALL
        SELECT  DATEADD(dd,1,CDATE),
        DATENAME(dw, CONVERT(varchar, DATEADD(dw,1,CDAY)))
        FROM    CTE_DATE
        WHERE   DATEADD(dd,1,CDATE) <= DATEADD(dd,-(DAY(DATEADD(mm,1,CDATE))),DATEADD(mm,1,CDATE))

    )
    SELECT * FROM CTE_DATE

5 个答案:

答案 0 :(得分:2)

无需转换为varchar即可获得工作日。

 UNION ALL
        SELECT  DATEADD(dd,1,CDATE),
        DATENAME(dw, CONVERT(varchar, DATEADD(dw,1,CDAY))) -- No need to convert to varchar in order to get weekday.
        FROM    CTE_DATE
        WHERE   DATEADD(dd,1,CDATE) <= DATEADD(dd,-(DAY(DATEADD(mm,1,CDATE))),DATEADD(mm,1,CDATE))

您可以使用datename函数直接获取它。

  DECLARE @V_DATE DATE = GETDATE()

    ;WITH CTE_DATE AS (
            SELECT  DATEADD(dd,-(DAY(@V_DATE)-1),@V_DATE) CDATE,
                    DATENAME(dw, CONVERT(varchar, DATEADD(dd,-(DAY(@V_DATE)-1),@V_DATE))) CDAY
            UNION ALL
            SELECT  DATEADD(dd,1,CDATE),
                    DATENAME(dw,  DATEADD(dd,1,CDATE)) -- modified 
            FROM    CTE_DATE
            WHERE   DATEADD(dd,1,CDATE) <= DATEADD(dd,-(DAY(DATEADD(mm,1,CDATE))),DATEADD(mm,1,CDATE))

        )
        SELECT * FROM CTE_DATE

答案 1 :(得分:2)

您可以很快使用datename()函数(自2008年起使用

  select datename( weekday, getdate() ) as day

  day
  ------
  Friday  -- > "for today(2019-04-26)"

Demo

或根据您的情况:

with t(cdate) as
(
 select '2019-04-01' union all
 select '2019-04-02' union all
 select '2019-04-30'    
)    
select cdate, datename( weekday, cdate  ) as cday
  from t;

+----------+-------+
| cdate    | cday  |
+----------+-------+
|2019-04-01|Monday |
|2019-04-02|Tuesday|
|2019-04-30|Tuesday|
+----------+-------+

答案 2 :(得分:2)

您的问题是:

DATENAME(dw, DATEADD(dw, 1, CDAY))

我认为您打算这样做

DATENAME(dw, DATEADD(dw, 1, CDATE))

我将CTE写为:

WITH CTE_DATE AS (
        SELECT  DATEADD(day ,-(DAY(@V_DATE)-1),@V_DATE) as CDATE, 
                DATENAME(dw, DATEADD(day, -(DAY(@V_DATE) - 1), @V_DATE)) as CDAY
        UNION ALL
        SELECT  DATEADD(day, 1, CDATE),
                DATENAME(dw, DATEADD(dw, 1, CDATE))
        FROM    CTE_DATE
        WHERE   DATEADD(day, 1, CDATE) <= DATEADD(day, -(DAY(DATEADD(month, 1, CDATE))), DATEADD(month, 1, CDATE))

    )
    SELECT *
    FROM CTE_DATE;

Here是db <>小提琴。

您没有描述您想要代码执行的代码。它具有不必要的字符串转换功能,对于您想做的事情可能会不必要地复杂。

答案 3 :(得分:2)

无需在CTE中固定日期名称,只需将其用于生成日期即可。

DECLARE @V_DATE DATE = GETDATE()

WITH CTE_DATE AS
(
    SELECT  DATEADD(day ,-(DAY(@V_DATE)-1),@V_DATE) as CDATE
    UNION ALL
    SELECT  DATEADD(day, 1, CDATE)
    FROM    CTE_DATE
    WHERE   DATEADD(day, 1, CDATE) <= DATEADD(day, -(DAY(DATEADD(month, 1, CDATE))), DATEADD(month, 1, CDATE))
)
SELECT CDATE, DATENAME(dw, CDATE) FROM CTE_DATE

答案 4 :(得分:2)

不需要CONVERT日期。使用FORMAT函数。并使用EOMONTH函数:

DECLARE @V_DATE DATE = DATEADD(DAY, 1, EOMONTH(GETDATE(), -1));
WITH CTE_DATE AS (
    SELECT @V_DATE CDATE

    UNION ALL

    SELECT DATEADD(dd, 1, CDATE)
    FROM   CTE_DATE
    WHERE  DATEADD(dd, 1, CDATE) <= EOMONTH(@V_DATE)
)
SELECT CDATE, FORMAT(CDATE, 'dddd') AS CDAY, FORMAT(CDATE, 'ddd') AS CDAYSHORT
FROM CTE_DATE