sqldateadd函数_DATEADDSQL函数简介和概述

更新时间:2023-06-08 09:34:28 阅读: 评论:0

sqldateadd函数_DATEADDSQL函数简介和概述
sql dateadd函数
This article explores the DATEADD SQL function and its usage scenarios with various examples.
本⽂通过各种⽰例探索DATEADD SQL函数及其使⽤⽅案。
Usually, we work with date data type in SQL Server. We need to do manipulations with dates as well. In my previous article , we explored to retrieve specific part from the date such as day, year, month, quarter, day of the year.
通常,我们在SQL Server中使⽤⽇期数据类型。 我们还需要对⽇期进⾏操作。 在上⼀篇⽂章 ,我们探索了从⽇期中检索特定的部分,例如⽇,年,⽉,季度,年中的某⽇。
We can u the SQL SERVER DATEADD function to add or subtract specific period from a gives a date.
我们可以使⽤SQL SERVER DATEADD函数从给定⽇期中添加或减去特定时间段。
句法 (Syntax )
DATEADD (datepart, number, date)
DATEADD(⽇期部分,数字,⽇期)
Datepart: It specifies the part of the date in which we want to add or subtract specific time interval. It can have values such as year, month, day, and week. We will explore more in this in the example ction Datepart :它指定要在其中添加或减去特定时间间隔的⽇期部分。 它可以具有诸如年,⽉,⽇和周的值。 我们将在⽰例部分中对此进⾏更多探索
Number: It is the number by which we want to increa or decrea date. It should be an integer 数字:这是我们要增加或减少⽇期的数字。 应该是整数
Date: We can specify the date here. In this date, we want to add a specified number on datepart ⽇期:我们可以在此处指定⽇期。 在此⽇期,我们要在datepart上添加⼀个指定的数字
Suppo we want to add ten days to the current date. In this example, the variables would be
假设我们要在当前⽇期前增加⼗天。 在此⽰例中,变量为
Day 天
5 5
Date: Getdate() – Current date
⽇期: Getdate() – 当前⽇期
通过⽰例探索SQL SERVER DATEADD函数 (Explore the SQL SERVER DATEADD function with examples)
Example Datepart Query with output
新年图片大全
Add 10 days to specified date dd
d
SELECT DATEADD(dd, 10, '2019/04/29');
Output – 2019-05-09 00:00:00.000
Subtract one day from a specified date dd
D
Output -2019-04-28 00:00:00.000
Add two years in a specified date YYYY
YY
SELECT DATEADD(YY,2, '2019/04/29');
Output – 2021-04-29 00:00:00.000
Subtract three years from a specified date YYYY
YY
Output – 2016-04-29 00:00:00.000
Add 4 Months in specified date MM
SELECT DATEADD(MM,5, '2019/04/29');
Output – 2019-09-29 00:00:00.000
Add 2 hours to a date HH Output – 2019-04-29 02:00:00.000
In this example, we did not specify any time with the input date. By default, SQL Server considers time 00:00.000
Subtract 120 minutes from date MM
SELECT DATEADD(Mi,-120, '2019/04/29')
Output – 2019-04-28 22:00:00.000
Add 1 quarter in specified
date
QQ Output – 2019-07-29 00:00:00.000
Add 100 days of the year in a specified date DY
SELECT DATEADD(DY,100, '2019/04/29')
人驴种子Output – 2019-08-07 00:00:00.000
Invalid value – Add
12578995 years in specified date Year
We cannot specify invalid values or out of range values in the SQL
SERVER DATEADD function. We get following error message Output
– Msg 517, Level 16, State 1, Line 1: Adding a value to a
‘datetime’ column caud an overflow.育苗幼儿园
Out of range number value – In the DATEADD number parameter, we can specify only integer values.Integer value range:
-231 (-
2,147,483,648) to
231-1
(2,147,483,647)
SELECT DATEADD(year,2147483648, '20060731');
In this example, we specified value 2147483648 in number
parameter. This value does not fall inside an integer range. Once we
execute this query, we get the following error message.
Msg 8115, Level 16, State 2, Line 3
Arithmetic overflow error converting expression to data type int.
例⽇期部分查询输出
在指定⽇期增加10天dd
d
产出– 2019-05-09 00:00:00.000
从指定⽇期减去⼀天dd
早餐吃什么最有营养
d
SELECT DATEADD(dd,-1, '2019/04/29');
输出 -2019-04-28 00:00:00.000
在指定的⽇期增加两年YYYY
YY
输出– 2021-04-29 00:00:00.000
从指定⽇期减去三年YYYY
YY
SELECT DATEADD(YY,-3, '2019/04/29');
产出– 2016-04-29 00:00:00.000
在指定⽇期增加4个⽉MM产出– 2019-09-29 00:00:00.000
将⽇期增加2个⼩时H
SELECT DATEADD(HH,2, '2019/04/29');
产出– 2019-04-29 02:00:00.000
在此⽰例中,我们没有使⽤输⼊⽇期指定任何时间。 默认情况下,SQL Server考虑时间00:00.000
从⽇期减去120分钟MM产出– 2019-04-28 22:00:00.000在指定⽇期增加1个季度QQ服从领导安排
SELECT DATEADD(QQ,1, '2019/04/29')
产出– 2019-07-29 00:00:00.000在指定⽇期添加⼀年中的
100天
DY产出 – 2019-08-07 00:00:00.000
值⽆效-在指定⽇期增加了12578995年年
SELECT DATEADD(year,12578995, '20060731');
我们不能在SQL SERVER DATEADD函数中指定⽆效值或超出范围的值。
我们收到以下错误消息输出 – 消息517,级别16,状态1,第1⾏:向“
datetime”列添加⼀个值导致溢出。
超出范围的数字值–在DATEADD数字参数中,我们只能指定整数值。整数值范围 :-231(-
2,147,483,648)到231-
1(2,147,483,647)
在此⽰例中,我们在number参数中指定了值2147483648。 该值不在整
数范围内。 ⼀旦执⾏此查询,我们将收到以下错误消息。
Msg 8115,第16级,州2,第3⾏
将表达式转换为数据类型int的算术溢出错误。
SQL SERVER DATEADD函数中的数据类型 (Data types in the SQL SERVER DATEADD function)
We can u the following data type in date expression.
我们可以在⽇期表达式中使⽤以下数据类型。
date
⽇期
DateTime
约会时间
datetimeofft
⽇期时间偏移
datetime2
datetime2
smalldatetime
⼩⽇期时间
time
时间
In the following query, we declared a variable @date having a datetime2 datatype. We can u the DATEADD SQL function as per the following query.
在以下查询中,我们声明了⼀个具有datetime2数据类型的变量@date。 我们可以按照以下查询使⽤DATEADD SQL函数。
DECLARE @Date datetime2;
SET @Date = '2019-04-29 01:01:01.1111111';
Select DATEADD(quarter,1,@Date),
DATEADD(month,11,@Date)
,DATEADD(dayofyear,265,@Date)
, DATEADD(day,265,@Date)
, DATEADD(week,4,@Date)
, DATEADD(weekday,29,@Date)
, DATEADD(hour,11,@Date)
, DATEADD(minute,59,@Date)
, DATEADD(cond,01,@Date)
, DATEADD(millicond,1,@Date);
使⽤SQL SERVER DATEADD函数从指定⽇期范围内的表中获取记录 (Using the SQL SERVER DATEADD function to get records from a table in specified date range)
We can u the DATEADD SQL function to retrieve records from a table for a period. In the following query, we specified a date with parameter @Startdate. We want to retrieve records from Orders table. We need to get records between
@StartDate and @Enddate ( add 1 hour in start date) .
我们可以使⽤DATEADD SQL函数从表中检索⼀段时间的记录。 在以下查询中,我们使⽤参数@Startdate指定了⼀个⽇期。 我们要
从“订单”表中检索记录。 我们需要获取@StartDate和@Enddate之间的记录(在开始⽇期中添加1⼩时)。
DECLARE @StartDate DATETIME= '2013-05-01 11:00.000';
DECLARE @Hours INT= 1;
SELECT OrderID,
LastEditedWhen
FROM [WideWorldImporters].[Sales].[Orders]春夜洛城闻笛的意思
WHERE LastEditedWhen BETWEEN @StartDate AND DATEADD(HOUR, @Hours, @StartDate);
We get the following output.
我们得到以下输出。
使⽤SQL SERVER DATEADD函数获取⽇期或时间差 (Using the SQL SERVER DATEADD function t
o get date or time difference)
甘蔗的营养价值
We can u the DATEADD SQL function to get time difference between the start date and end date. Many times, we want to u the SQL SERVER DATEADD function to get date difference. For example, we want to know how long it took to complete an order or how long it took to reach from home to office.
我们可以使⽤DATEADD SQL函数获取开始⽇期和结束⽇期之间的时差。 很多时候,我们想使⽤SQL SERVER DATEADD函数来获取⽇期差。 例如,我们想知道完成订单需要多长时间,或者从家到办公室要花费多长时间。
Execute the following query to get the difference between starttime and endtime. We u the DATEADD SQL function along with the DATEDIFF SQL function.
执⾏以下查询以获取开始时间和结束时间之间的差异。 我们将DATEADD SQL函数与DATEDIFF SQL函数⼀起使⽤。
DECLARE @StartTime DATETIME= '2019-04-29 20:30:00', @EndTime DATETIME= '2019-04-30 01:00:00';
SELECT DATEADD(Minute,DATEDIFF(Minute, @StartTime, @EndTime),0) AS ElapdTime;
It gives the elapd time in minutes. We specified value 0 in the DateADD SQL function. It takes date value 1900-01-01
00:00:00.000
它以分钟为单位给出经过的时间。 我们在DateADD SQL函数中指定了值0 。 它需要⽇期值1900-01-01 00:00:00.000
We can u format to reprent this in HH:MM:SS format.
我们可以使⽤格式将其表⽰为HH:MM:SS格式。
DECLARE @StartTime DATETIME= '2019-04-29 20:30:00', @EndTime DATETIME= '2019-04-30 01:00:00';
SELECT CONVERT(VARCHAR(8), DATEADD(Minute, DATEDIFF(Minute, @StartTime, @EndTime), 0), 108) AS ElapdTime;
It gives the time difference in HH:MM:SS format as shown in the following image.
如下图所⽰,它以HH:MM:SS格式给出了时差。
将SQL SERVER DATEADD函数结果指定为新列 (Specify the SQL SERVER DATEADD function result as a new column )
We can u the SQL SERVER DATEADD function to get a new column after adding the required date value. In the following query, we want to add two days in the start date and reprent this as a new column.
添加所需的⽇期值后,我们可以使⽤SQL SERVER DATEADD函数获取新列。 在以下查询中,我们要在开始⽇期添加两天,并将其表⽰为新列。
DECLARE @StartDate DATETIME= '2013-05-01 11:00.000';
DECLARE @Day INT= 2;
SELECT OrderID,
LastEditedWhen,
DATEADD(day, @Day, LastEditedWhen) AS NewDateColumn
FROM [WideWorldImporters].[Sales].[Orders];
赤脚走路DATEADD SQL函数中的标量⼦查询和标量函数 (Scalar sub queries and scalar functions in the DATEADD SQL function)

本文发布于:2023-06-08 09:34:28,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/901527.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:函数   指定   时间   添加
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图