什么是SQL Server DATEDIFF()方法?

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

嘿伙计! 在本文中,我们将重点关注 SQL Server DATEDIFF()函数的详细信息。



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

DATEDIFF() helps us estimate the difference between dates passed as a parameter to the function.

DATEDIFF()帮助我们估计作为参数传递给函数的日期之间的差异。

The function calculates the difference between the dates in terms of years, months, days, or time.

该函数以年,月,日或时间为单位计算日期之间的差异。



SQL DATEDIFF()函数的语法 (Syntax of SQL DATEDIFF() function)

In order to operate the DATEDIFF() function, we need to follow the below command.

为了操作DATEDIFF()函数,我们需要遵循以下命令。


DATEDIFF(date_section, 'date1', 'date2');
  • date_section: This is the portion of the date and timestamp of which the comparison needs to be made.

    date_section :这是日期和时间戳记中需要进行比较的部分。

The following date_section can be used for comparison:

以下date_section可用于比较:

date_section
year
second
month
week
day
dayofyear
hour
minute
quarter
millisecond
microsecond
nanosecond
date_section
第二
每年的一天
小时
分钟
25美分硬币
毫秒
微秒
纳秒
SQL Server DATEDIFF() method — date_section SQL Server DATEDIFF()方法— date_section

The format of the date values passed is as follows

传递的日期值的格式如下

yyyy/mm/dd

yyyy / mm / dd

Having understood the working of SQL DATEDIFF() function, lets us get started with the implementation of the same.

了解了SQL DATEDIFF()函数的工作原理之后,我们就开始实现该功能。



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

Let us now analyze the implementation and execution of the DATEDIFF() function.

现在让我们分析DATEDIFF()函数的实现和执行。



SQL DATEDIFF()函数以“年”为间隔 (SQL DATEDIFF() function with ‘year’ as an interval)


SELECT DATEDIFF(year, '2020/5/20', '2025/10/15') AS Output;

In this example, we have passed ‘year’ as the interval and this leads to the difference between the years passed in the function parameter.

在此示例中,我们以“年”作为间隔,这导致函数参数中传递的年之间存在差异。

Output:

输出:


5


DATEDIFF()函数,以“月”为间隔 (DATEDIFF() function with ‘month’ as an interval)


SELECT DATEDIFF(month, '2020/5/20', '2020/10/15') AS Output;

The above example returns the difference between the months of the passed date intervals.

上面的示例返回经过日期间隔的月份之间的差。

Output:

输出:


5


SQL DATEDIFF()函数以“ dayofyear”为间隔 (SQL DATEDIFF() function with ‘dayofyear’ as an interval)

Example:

例:


SELECT DATEDIFF(dayofyear, '2020/5/20', '2020/10/15') AS Output;

Output:

输出:


148


SQL DATEDIFF()函数,以“小时”为间隔 (SQL DATEDIFF() function with ‘hour’ as an interval)

The interval hour leads to calculate the difference between the hours of the two passed timestamps.

间隔小时导致计算两个经过的时间戳之间的小时差。

Example:

例:


SELECT DATEDIFF(hour, '2020/5/20 08:00', '2020/5/20 15:00') AS Output;

Output:

输出:


7


SQL Server DATEDIFF()函数以“周”为间隔 (SQL Server DATEDIFF() function with ‘week’ as an interval)

The week interval calculates the number of weeks present between the two given timestamps.

week间隔计算两个给定时间戳之间存在的周数。

Example:

例:


SELECT DATEDIFF(week, '2020/5/20', '2020/6/20') AS Output;

Output:

输出:


4


摘要 (Summary)

  • SQL Server DATEDIFF() function deals with the date and time values as parameters.

    SQL Server DATEDIFF()函数将日期和时间值作为参数处理。
  • Moreover, if we try to pass some non-date format values to the function, an error is raised by the compiler.

    此外,如果我们尝试将一些非日期格式的值传递给该函数,则编译器会引发错误。
  • The DATEDIFF() function is essential when we need to estimate the difference between different date_section of the timestamps.

    当我们需要估计时间戳的不同date_section之间的差异时,DATEDIFF()函数必不可少。


结论 (Conclusion)

By this, we have come to an end of this topic. I hope the entire topic is clear to all the readers. Please feel free to comment in case you come across a doubt.

至此,我们结束了本主题。 我希望整个主题对所有读者都清楚。 如果您有任何疑问,请随时发表评论。

For more such SQL related posts, please do visit SQL JournalDev.

有关此类SQL相关文章的更多信息,请访问SQL JournalDev



参考资料 (References)

翻译自: https://www.journaldev.com/40789/sql-datediff-function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值