sql dateadd函数_什么是SQL Server DATEADD()函数?

sql dateadd函数

Hey, folks! In this article, we will be focusing on SQL Server DATEADD() function in detail.

嘿伙计! 在本文中,我们将重点关注SQL Server DATEADD()函数

什么是SQL DATEADD()函数? (What is SQL DATEADD() function?)

SQL Server DATEADD() function adds particular value to the date input passed to the function and returns the updated/modified value.

SQL Server DATEADD() function将特定值添加到传递给该函数的日期输入中,并返回更新/修改后的值。

The DIFFADD() function adds a customized value to the portion of timestamp such as year, days, month, week, hours, mins, etc.

DIFFADD()函数将自定义值添加到时间戳部分,例如年,日,月,周,小时,分钟等。



SQ DATEADD()函数的语法 (Syntax of SQ DATEADD() function)


DATEADD(date_section, interval, date);
  • date_section: This includes the portion of the date to which the value would be added.

    date_section :这包括日期中要添加值的部分。
  • interval: It refers to the value that has to be added to the date portion.

    interval :它是必须添加到日期部分的值。
  • date: The date that needs to be passed in the form of “yyyy/mm/dd“.

    date :需要以“ yyyy / mm / dd ”形式传递的日期。

The DATEADD() function would return the updated timestamp after adding the interval value to it.

DATEADD()函数将在其上添加间隔值后将返回更新的时间戳。



SQL Server DATEADD()的示例 (Examples of SQL Server DATEADD())

After having understood the syntax of SQL Server DATEADD() function, let us implement the function through various examples.

在了解了SQL Server DATEADD()函数的语法之后,让我们通过各种示例来实现该函数。



SQL DATEADD()函数,以“ year”作为date_section (SQL DATEADD() function with ‘year’ as the date_section)

In the below example, we have used ‘year’ as the date portion with the interval of 1 i.e. 1 would be added to the year of the passed timestamp.

在下面的示例中,我们使用“年”作为日期部分,间隔为1,即1将被添加到所传递时间戳的年份中。


SELECT DATEADD(year, 1, '2020/10/15') AS Output;

Output:

输出:


2021-10-15T00:00:00Z


SQL Server DATEADD()函数以“ hour”作为date_section (SQL Server DATEADD() function with ‘hour’ as the date_section)

In this example, we have used ‘hour’ as the date_section i.e. the provided interval would be added to the hour of the timestamp.

在此示例中,我们使用“小时”作为date_section,即将提供的间隔添加到时间戳的小时中。


SELECT DATEADD(hour, 2, '2020/10/15 17:20:20') AS Output;

Output:

输出:


2020-10-15T19:20:20Z


SQL DATEADD()函数以“ month”作为date_section (SQL DATEADD() function with ‘month’ as the date_section)

Here, we have used month as the date section and an interval of 2 will get added to the month of the passed timestamp.

在这里,我们使用month作为日期部分,并将2的间隔添加到传递的时间戳的月份中。


SELECT DATEADD(month, 2, '2020/10/15 17:20:20') AS Output;

Output:

输出:


2020-12-15T17:20:20Z


SQL DATEADD()函数以“ dayofyear”作为date_section (SQL DATEADD() function with ‘dayofyear’ as the date_section)

In this example, we have used dayofyear as the date_section by which the interval adds up to the date value of the timestamp.

在此示例中,我们将dayofyear用作date_section,间隔将其相加起来成为时间戳的日期值。


SELECT DATEADD(dayofyear, 2, '2020/10/15 17:20:20') AS Output;

Output:

输出:


2020-10-17T17:20:20Z


具有负间隔值SQL Server DATEADD()函数 (SQL Server DATEADD() function with a negative interval value)

Apart from adding interval to the timestamp/date, we can even reduce the date values by subtracting the interval values using a negative interval.

除了在时间戳记/日期中添加间隔外,我们甚至可以通过使用负间隔减去间隔值来减少日期值。


SELECT DATEADD(year, -2, '2020/10/15 17:20:20') AS Output;

As a result, the ‘year’ reduces by an interval of 2 in the output.

结果,“年”在输出中减少了2的间隔。

Output:

输出:


2018-10-15T17:20:20Z


摘要 (Summary)

SQL Server DATEADD() function is extensively used when we want to sum up the current timestamp with a particular value for summarization and predictions through the database.

当我们希望将当前时间戳与特定值进行汇总以通过数据库进行汇总和预测时,将广泛使用SQL Server DATEADD()函数。

The DATEADD() function is used to add or subtract intervals from the date passed in the parameter list.

DATEADD()函数用于从参数列表中传递的日期增加或减少间隔。



结论 (Conclusion)

By this, we have come to the end of this topic. Please feel free to comment in case you come across any doubt.

至此,我们到了本主题的结尾。 如果您有任何疑问,请随时发表评论。

For more of such topics related to SQL, please do visit SQL Server JournalDev.

有关与SQL有关的更多此类主题,请访问SQL Server JournalDev



参考资料 (References)

翻译自: https://www.journaldev.com/40786/sql-dateadd-function

sql dateadd函数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值