gpt4 book ai didi

sql - Datepart 和 @@datefirst 的区别

转载 作者:行者123 更新时间:2023-12-03 03:13:20 26 4
gpt4 key购买 nike

据我了解,DateFirst 是一周的第一天,或者一周开始的那一天。但是,当我运行这些查询时,我的结果与我认为的理解不符

select @@datefirst,DATENAME(WEEKDAY,@@datefirst)
SELECT DATEPART(WEEKDAY,'20190113'),DATENAME(WEEKDAY,'20190113')
SELECT DATEPART(WEEKDAY,'20190114'),DATENAME(WEEKDAY,'20190114')
SELECT DATEPART(WEEKDAY,'20190115'),DATENAME(WEEKDAY,'20190115')
SELECT DATEPART(WEEKDAY,'20190116'),DATENAME(WEEKDAY,'20190116')
SELECT DATEPART(WEEKDAY,'20190117'),DATENAME(WEEKDAY,'20190117')
SELECT DATEPART(WEEKDAY,'20190118'),DATENAME(WEEKDAY,'20190118')
SELECT DATEPART(WEEKDAY,'20190119'),DATENAME(WEEKDAY,'20190119');

我明白了

7 Monday (this is the @@datefirst line)
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday

@@datefirst 如何给我 7,而这一天给我星期一,而 7 是星期六?这是配置问题还是我的误解?

最佳答案

基于以下Microsoft Documentation :

This function returns a character string representing the specified datepart of the specified date.

Syntax: DATENAME ( datepart , date )

<小时/>

更新 1

在同一篇文章的以下部分中:为不在日期参数中的日期部分返回默认值

If the data type of the date argument does not have the specified datepart, DATENAME will return the default for that datepart only if the date argument has a literal .

For example, the default year-month-day for any date data type is 1900-01-01. This statement has date part arguments for datepart, a time argument for date, and DATENAME returns 1900, January, 1, 1, Monday

因此,当您输入数字7时,输入将被视为1900-01-01 07:00:00,返回星期一

如果您转到 SQL Server 并运行以下命令

select DATENAME(WEEKDAY,'1900-01-01 07:00:00')

返回

Monday

关于sql - Datepart 和 @@datefirst 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com