Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql DATEADD的这三种用法有什么区别吗?_Sql_Sql Server 2005_Tsql - Fatal编程技术网

Sql DATEADD的这三种用法有什么区别吗?

Sql DATEADD的这三种用法有什么区别吗?,sql,sql-server-2005,tsql,Sql,Sql Server 2005,Tsql,下面的三条语句都返回相同的结果。所以我很难看出使用dayofyear、day和weekday之间的区别。是我遗漏了什么,还是它们都是等价的 SELECT DATEADD(dayofyear,1,'20111231') SELECT DATEADD(day,1,'20111231') SELECT DATEADD(weekday,1,'20111231') 它们都返回2012-01-01 00:00:00.000在该用途中,不返回相同的值。虽然使用“一天”感觉很自然 不同之处在

下面的三条语句都返回相同的结果。所以我很难看出使用dayofyear、day和weekday之间的区别。是我遗漏了什么,还是它们都是等价的

   SELECT DATEADD(dayofyear,1,'20111231')
   SELECT DATEADD(day,1,'20111231')
   SELECT DATEADD(weekday,1,'20111231')

它们都返回2012-01-01 00:00:00.000

在该用途中,不返回相同的值。虽然使用“一天”感觉很自然

不同之处在于(例如)日期部分:

SELECT DATEPART(dayofyear,'20111231') -- 365
SELECT DATEPART(day,'20111231') -- 31
SELECT DATEPART(weekday,'20111231') --7
下面是上面的说明:

dayofyear、day和weekday返回 相同的值

它们都是等价的

日期部分月、年、日、小时都是各自的度量单位。然而,dayofyear和weekday不是。但你可以这样看-

the datepart "month" changes when the month changes,
the datepart "hour" changes when the hour changes,
the datepart "dayofyear" changes (by 1) when the day changes,
the datepart "weekday" changes (by 1) when the day changes
the datepart "day" changes (by 1) when the day changes
所以你不得不说,一年中的“日”单位等于一天。要添加“dayofyear”部分,它会将日期增加1天

如果您以日期变量或日期/时间列开始,则只需向其中添加1,例如

dateadd(d, 1, @date)  ==  @date + 1
它不适用于日期文字

select '2010-12-12' + 1   -- attempts to add the string '2010-12-12' to 1
一个人必须施放/转化

select cast('2010-12-12' as datetime) + 1
但到那个阶段,您还可以使用dateadd,它隐式地强制转换第三个参数

select dateadd(d, 1, '2010-12-12')  -- 2010-12-13
select dateadd(d, 1, 40522)         -- 2010-12-13
select dateadd(d, 1, '2010-12-12')  -- 2010-12-13
select dateadd(d, 1, 40522)         -- 2010-12-13