SQL 如何从日期显示一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4492568/
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-09-01 08:38:43  来源:igfitidea点击:

How to display a day from the Date

sqlsql-serversql-server-2005tsqldate-formatting

提问by Gopal

Using SQL Server 2005

使用 SQL Server 2005

Table1

表格1

Date

19-12-2009
20-12-2010
.....

Date Column datatype is DATETIME.

日期列数据类型是DATETIME.

Expected Output

预期产出

Monday
Tuesday

How to make a query for getting the day...

如何查询获取日期...

回答by Lamak

You can use the DATENAMEfunction.

您可以使用该DATENAME功能。

SELECT DATENAME(WEEKDAY,[Date])
FROM Table1

回答by Dan J

As @Lamak suggested, you can use the DATENAME functionif you're on SQL Server 2005 or later:

正如@Lamak 所建议的,如果您使用的是 SQL Server 2005 或更高版本,则可以使用 DATENAME函数

SELECT DATENAME(dw, DateField) FROM Table

On earlier versions, the closest you could get was the DATEPART functionwith dwas the interval, but you needed a CASE to convert the number returned to the day name, as seen here.

在早期版本中,你可以得到最接近的是DATEPART功能dw作为间隔,但你需要一个CASE转换回到天名的数量,因为在这里看到

SELECT CASE DATEPART(dw, DateField)
       WHEN 1 THEN 'Sunday'
       WHEN 2 THEN 'Monday'
       WHEN 3 THEN 'Tuesday'
       WHEN 4 THEN 'Wednesday'
       WHEN 5 THEN 'Thursday'
       WHEN 6 THEN 'Friday'
       WHEN 7 THEN 'Saturday'
       END AS DayOfWeek
FROM Table

Also note the remarks on the MSDN documentation for those date functions, for information about specifying which day of the week is considered the firstday (depends on the DATEFORMATsetting of your SQL environment).

另请注意 MSDN 文档中关于这些日期函数的备注,了解有关指定一周中哪一天被视为第一天的信息(取决于DATEFORMATSQL 环境的设置)。

回答by John Hartsock

Here is the reference for DateNamewhich will assist you

这是DateName的参考,它将帮助您

http://msdn.microsoft.com/en-us/library/ms174395.aspx

http://msdn.microsoft.com/en-us/library/ms174395.aspx

If you want a numerical Reference like Sunday = 1, Saturday = 7, then use DatePart

如果您想要一个数字参考,如周日 = 1、周六 = 7,则使用DatePart

http://msdn.microsoft.com/en-us/library/ms174420.aspx

http://msdn.microsoft.com/en-us/library/ms174420.aspx

This is what you want

这就是你想要的

DateName(dw, [Date])

回答by Dr.Luiji

I'd like to suggest to set the language before get the week day

我想建议在获得工作日之前设置语言

SET LANGUAGE 'Italian'
SELECT DATENAME(WEEKDAY,GETDATE())

回答by navya

select name from table where weekday(column_name = 'monday');

It prints whoever born on monday it displays their names

它打印星期一出生的人,并显示他们的名字