74

I am trying get a day name like friday, saturday, sunday, monday etc from a given date. I know there is a built in function which returns the day name for example:

SELECT DATENAME(dw,'09/23/2013') as theDayName 

this SQL query returns:

'Monday'

This is all OK. But I would like to pass Month, Day and Year individually.

I am using the builtin DATEPART function to retrieve month, day and year from a date so I can pass it to the DATENAME function:

SELECT DATEPART(m, GETDATE()) as theMonth  -- returns 11
SELECT DATEPART(d, GETDATE()) as theDay   -- returns 20
SELECT DATEPART(yy, GETDATE()) as theYear   -- returns 2013

Now that I have Month, Day, Year values individually, I pass it to my DATENAME to get the Weekname of the date I want:

--my SQL query to return dayName
SELECT (DATENAME(dw, DATEPART(m, GETDATE())/DATEPART(d, myDateCol1)/ DATEPART(yy, getdate())))  as myNameOfDay, FirstName, LastName FROM myTable

This returns an incorrect Day Name. I tried replace / with - so that in the DATENAME function my SQL query becomes:

SELECT DATENAME(dw,'09/23/2013') 
--becomes
SELECT DATENAME(dw,'09-23-2013') 

but it still returns incorrect dayName from my SQL query. Am I missing something here.

Please advise.

1
  • Please be aware of select @@DATEFIRST which is set SET DATEFIRST 7;
    – SQLMason
    Jul 29, 2016 at 14:25

7 Answers 7

81

Tested and works on SQL 2005 and 2008. Not sure if this works in 2012 and later.

The solution uses DATENAME instead of DATEPART

select datename(dw,getdate()) --Thursday
select datepart(dw,getdate()) --2

This is work in sql 2014 also.

0
44

You need to construct a date string. You're using / or - operators which do MATH/numeric operations on the numeric return values of DATEPART. Then DATENAME is taking that numeric value and interpreting it as a date.

You need to convert it to a string. For example:

SELECT (
  DATENAME(dw, 
  CAST(DATEPART(m, GETDATE()) AS VARCHAR) 
  + '/' 
  + CAST(DATEPART(d, myDateCol1) AS VARCHAR) 
  + '/' 
  + CAST(DATEPART(yy, getdate()) AS VARCHAR))
  )
3
  • 12
    select DATENAME(DW,GETDATE()); This is enough to get day name. Apr 30, 2018 at 12:31
  • 4
    That wasn't the issue or question, @AuthorPraWin May 1, 2018 at 5:59
  • 3
    @EliGassert granted, but this is the top Stack Overflow result in Google for the term 'tsql day name' and this answer along with AuthorPraWin's useful addendum helped me immediately.
    – A. Murray
    Jun 8, 2018 at 10:05
8

If you have SQL Server 2012:

If your date parts are integers then you can use DATEFROMPARTS function.

SELECT DATENAME( dw, DATEFROMPARTS( @Year, @Month, @Day ) )

If your date parts are strings, then you can use the CONCAT function.

SELECT DATENAME( dw, CONVERT( date, CONCAT( @Day, '/' , @Month, '/', @Year ), 103 ) )
7

Try like this: select DATENAME(DW,GETDATE())

1
SELECT DATENAME(DW,CONVERT(VARCHAR(20),GETDATE(),101))
1
  • 5
    Please provide some more details (e.g. a simple explanation of what you SQL script does) to your answer.
    – rbr94
    Aug 25, 2017 at 11:59
0

select to_char(sysdate,'DAY') from dual; It's work try it

2
  • The database that OP is using is SQL Server. Your response is for Oracle. It will not work.
    – Ergi Nushi
    Jan 20, 2021 at 14:02
  • In the question, it's already mentioned that DB is SQL. You wrote it for Oracle
    – Mak
    Dec 6, 2021 at 7:31
-1

I used

select
case
when (extract (weekday from DATE)=0) then 'Sunday'

and so on...

0 Sunday, 1 Monday...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.