Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show day name in SQL Server?

I have a query on the SQL server

I want to display as below:

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

But I found error as below:

Conversion failed when converting date and/or time from character string.

Please if someone can help

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
like image 441
Momo Avatar asked Apr 26 '19 11:04

Momo


People also ask

How do I get the day of the week name in SQL?

To extract the day name, the interval must have one of the following values: weekday, dw , or w . All will return the same result. Take a look at the example: SELECT DATENAME(WEEKDAY, '2022-01-01' );

How do I declare a day in SQL?

MySQL comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS.

Which SQL function is used to print name of day?

The DAYNAME() function returns the weekday name for a given date.


3 Answers

Your problem is:

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

I think you intend:

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

I would write the CTE as:

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 is a db<>fiddle.

You don't describe what you want the code the code to do. It has unnecessary conversions to string and might be needlessly complicated for what you want to do.

like image 148
Gordon Linoff Avatar answered Oct 06 '22 00:10

Gordon Linoff


You can shortly use datename() function( used since v.2008 )

  select datename( weekday, getdate() ) as day

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

Demo

or as in your case :

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|
+----------+-------+
like image 42
Barbaros Özhan Avatar answered Oct 05 '22 22:10

Barbaros Özhan


No need to convert to varchar in order to get weekday.

 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))

you can directly get it using datename function.

  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
like image 22
ch2019 Avatar answered Oct 06 '22 00:10

ch2019