-1

As the title says I'm looking for a function in SQL-Server to retrieve all weekdays, based on a variable set as a year.

I know there should be two useful functions for this case, but I don't know how to use them properly nor I am good at writing functions in SQL:

SELECT DATENAME(dw, GETDATE()) and SELECT DATEPART(dw, GETDATE())

I am expecting something like this:

--------------------------
| DATA       | GIORNO    |
--------------------------
| 01/01/2020 | Wednesday |
--------------------------
| 02/01/2020 | Thursday  |
--------------------------

EDIT:

Suraj Kumar gave me a suggestion and I'm stuck here:

DECLARE @date date = getdate();

SELECT FORMAT(@date, 'd') AS DATA
SELECT DATENAME(WEEKDAY, @date) AS GIORNO

What I need to do now is to convert the DATA field to the italian format (day/month/year) and return a table with two field like I've explained before this edit...

Akr0n
  • 13
  • 1
  • 6
  • 4
    So what's *wrong* with `DATENAME`? That appears to do *exactly* what you want. `DATENAME(WEEKDAY,'20200101')` returns `'Wednesday'`. – Thom A Jan 29 '20 at 14:34
  • 2
    What does this talk about `year` have anything to do with the question? Sample data and desired results would help out quite a bit here. Your current sample/desired-results are easily solved with `DateName` so I'm suspecting there is more to the question than that. – JNevill Jan 29 '20 at 14:37
  • What you will pass like from date and to date or a week number in an integer. – Suraj Kumar Jan 29 '20 at 14:37
  • Dates don't have a format, they are (stored as) binary values. The format of the date is defined in the presentation layer. – Thom A Jan 29 '20 at 16:13
  • For display purpose you can convert date in different format as varchar as shown on this link https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/ – Suraj Kumar Jan 30 '20 at 04:45

1 Answers1

0

You can use one of the functions as shown below if you want to get a week name for a given date in SQL Server.

DECLARE @date datetime = getdate();
SELECT FORMAT(@date, 'dddd') AS Result;

SELECT 
    FORMAT(@date, 'dddd') as Result

SELECT DATENAME(weekday, @date) AS Result

SELECT {fn DAYNAME(@date)} AS Result;
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42