在 SQL Server 中分别从给定的月、日和年获取工作日名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20106871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:09:42  来源:igfitidea点击:

Get week day name from a given month, day and year individually in SQL Server

sqlsql-serverfunctiondate

提问by theITvideos

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:

此 SQL 查询返回:

'Monday'

'周一'

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

这一切都可以。但我想Month, Day and Year单独通过。

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:

我使用内置的 DATEPART 函数从日期中检索月、日和年,以便我可以将它传递给 DATENAME 函数:

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 DATENAMEto get the Weeknameof the date I want:

现在我分别有 Month、Day、Year 值,我将它传递给 myDATENAME以获得Weekname我想要的日期:

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

这将返回不正确的日期名称。我尝试用 - 替换 / 以便在 DATENAME 函数中我的 SQL 查询变为:

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.

但它仍然从我的 SQL 查询中返回不正确的 dayName。我在这里错过了什么。

Please advise.

请指教。

采纳答案by Eli Gassert

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 DATENAMEis taking that numeric value and interpreting it as a date.

您需要构造一个日期字符串。您正在使用/-运算符对 DATEPART 的数字返回值执行数学/数字运算。然后DATENAME取该数值并将其解释为日期。

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

回答by Irfan Raza

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

在 SQL 2005 和 2008 上测试和工作。不确定这是否在 2012 年及以后工作。

The solution uses DATENAME instead of DATEPART

解决方案使用 DATENAME 而不是 DATEPART

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

This is work in sql 2014 also.

这也是 sql 2014 中的工作。

回答by Greenstone Walker

If you have SQL Server 2012:

如果您有 SQL Server 2012:

If your date parts are integers then you can use DATEFROMPARTSfunction.

如果您的日期部分是整数,那么您可以使用DATEFROMPARTS函数。

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

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

如果您的日期部分是字符串,那么您可以使用该CONCAT函数。

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

回答by Praveen Patel G

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

像这样尝试: select DATENAME(DW,GETDATE())

回答by Chandra Sekhar Reddy Ginuga

SELECT DATENAME(DW,CONVERT(VARCHAR(20),GETDATE(),101))

回答by Leo Fazzi

I used

我用了

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

and so on...

等等...

0 Sunday, 1 Monday...

0个星期日,1个星期一...