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)

  • SQL Server DATEADD() function — DocumentationSQL Server DATEADD()函数—文档

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

sql dateadd函数

sql dateadd函数_什么是SQL Server DATEADD()函数?相关推荐

  1. 数据库创建函数_达梦数据库创建UUID函数

    数据库创建函数_达梦数据库创建UUID函数 接触达梦数据库有一段时间了,整理了一些资料,今天分享一下达梦数据UUID自定义函数 UUID函数定义 很多数据库都有提供UUID函数,可是接触达梦数据库后, ...

  2. mysql开窗函数_魔幻的SQL开窗函数,为您打开进阶高手的一扇天窗

    经常写SQL脚本的朋友,通常会有一种迷之自信,似乎各种问题都有自己的一套解决方案.时间长了,人的思维可能会逐渐固化.思维固化能提高工作效率,但从某些角度看是很可怕的,我们也同时会失去接受新知识的内在动 ...

  3. sql date 函数_什么是SQL DATE()函数?

    sql date 函数 Hey, folks! In this article, we will be focusing on SQL DATE() function in detail. So, l ...

  4. sql instr函数_如何实现SQL INSTR()函数?

    sql instr函数 Hey, folks! In this article, we will be understanding SQL INSTR() function in detail. 嘿伙 ...

  5. in ms sql 集合参数传递_神奇的 SQL → 为什么 GROUP BY 之后不能直接引用原表中的列?...

    GROUP BY 后 SELECT 列的限制 标准 SQL 规定,在对表进行聚合查询的时候,只能在 SELECT 子句中写下面 3 种内容:通过 GROUP BY 子句指定的聚合键.聚合函数(SUM ...

  6. sql 删除依赖_关系数据库标准语言SQL(二)

    声明:最近在准备考试,故整理数据库原理笔记. 视图 视图的创建和删除 CREATE VIEW <视图名 > [ <列名 > , --, <列名 >)] AS &l ...

  7. java防止sql注入方正_有效防止SQL注入的5种方法总结

    sql注入入门 SQL 注入是一类危害极大的攻击形式.虽然危害很大,但是防御却远远没有XSS那么困难. SQL 注入漏洞存在的原因,就是拼接 SQL 参数.也就是将用于输入的查询参数,直接拼接在 SQ ...

  8. sql int 比较_高质量SQL的30条建议 建议你收藏

    前言 本文将结合实例demo,阐述30条有关于优化SQL的建议,多数是实际开发中总结出来的,希望对大家有帮助. 查询SQL尽量不要使用select *,而是select具体字段. 反例子: selec ...

  9. exit函数_全面深入了解 Python 魔法函数

    (点击上方公众号,可快速关注一起学Python) 作者:浪子燕青       链接: http://www.langzi.fun/Python魔法函数.html 魔法函数概念 魔法函数是以双下划线开头 ...

最新文章

  1. Linux shell 学习笔记(7)— 构建基本脚本(变量、重定向、管道、状态码)
  2. SAP中执行没有权限的事务
  3. 信息系统管理工程师考前复习笔记三
  4. 手把手教渗透测试人员打造.NET可执行文件
  5. UNIX再学习 -- 线程同步
  6. 那些年,我们一起做过的性能优化
  7. Java并发程序设计(十一)设计模式与并发之生产者-消费者模式
  8. 用 Docker 构建 Serverless 应用
  9. vue怎么插入接口demo_前端学起来特别吃力,新人入前端怎么学?
  10. 挖掘城市ip_德国卡尔斯草莓农庄:旅游IP打造要创意更要形象!
  11. DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi
  12. sea.js常用接口
  13. Linux环境下编程有哪些优势?
  14. 如何用阿里云服务器建立一个wordpress网站
  15. VBA Excel 实践(三)Excel控件及Excel控件的初始化
  16. AppThinning之AppSlicing
  17. 「解析」netron 模型可视化
  18. miniUI ExcelExport导出JAVA实现
  19. 世界上第一次网络瘫痪 | 历史上的今天
  20. 众筹系统源码 java_以太坊 众筹系统

热门文章

  1. SCC会员北京车展围拍超跑 却火了这款华为MateRS保时捷版手机
  2. 【Python入门】Turtle海龟库:利用海龟画笔绘制花朵
  3. 经验分享——关于大学生科研那些事
  4. 精彩WAP之旅--上海热线WAP站
  5. 如果哥白尼也玩“虚拟化”和“超融合”
  6. 爬虫beautifulsoup爬取豆瓣读书数据
  7. smartSVN 分支合并到主干上,iOS 编译时链接错误
  8. w10计算机运行特别卡,Win10电脑运行卡死怎么办?Win10电脑卡死的解决方法
  9. GitHub 值得收藏的前端项目
  10. 6-2对象作为数据成员