什么是SQL Server DATEPART()方法?

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

嘿伙计! 在本文中,我们将重点介绍SQL Server DATEPART()函数



SQL DATEPART()方法的工作 (Working of SQL DATEPART() method)

SQL DATEPART() method helps to extract the portion from the standard timestamp structure which is,

SQL DATEPART() method有助于从标准时间戳结构中提取部分,

yyyy-mm-dd hours-mins-sec

yyyy-mm-dd小时-分钟-秒

The DATEPART() method extracts and helps in representing the date portions of the timestamp in the form of years, months, days, weeks, hours, mins, etc.

DATEPART()方法提取并帮助以年,月,日,周,小时,分钟等形式表示时间戳的日期部分。

Let us understand the format for each date portion that can be extracted from the date through the below table.

让我们了解可通过下表从日期中提取的每个日期部分的格式。

date_portionFormat
yearyy, yyyy
monthmm, m
second ss, s
daydd, d
weekwk, ww
weekdaydw
hourhh
nanosecondns
dayofyeardy, y
millisecondms
microsecondmcs
minutemi, n
date_portion 格式
yy yyyy
毫米,米
第二 ss,s
dd,d
ww,ww
平日 dw
小时 h
纳秒 ns
每年的一天 dy,y
毫秒 多发性硬化症
微秒 mcs
分钟
DATEPART() method date interval format DATEPART()方法日期间隔格式


SQL DATEPART()方法的语法 (Syntax of SQL DATEPART() method)

Having understood the working of SQL DATEPART() method, let us now look at the implementation of the mentioned function.

了解了SQL DATEPART()方法的工作原理后,现在让我们看一下所提到函数的实现。


DATEPART(date_portion, date)

As seen above, the DATEPART() function accepts the date_portion that is the portion to extract out of the date and the input timestamp. The function returns the extracted date portion from the

如上所示,DATEPART()函数接受date_portion,它是要从日期和输入时间戳中提取的部分。 该函数返回从中提取的日期部分



SQL DATEPART()方法的示例 (Examples of SQL DATEPART() method)

Now, let us understand the working behind the SQL Server DATEPART() function with the help of various examples stated below.

现在,让我们借助下面所述的各种示例来了解SQL Server DATEPART()函数背后的工作。



SQL DATEPART()方法,以“年”为间隔 (SQL DATEPART() method with ‘year’ as an interval)

In this example, we have passed ‘year’ as the date_portion to be extracted from the input date passed to the function.

在此示例中,我们已经传递了'year'作为date_portion,以从传递给函数的输入日期中提取出来。

Example:

例:


SELECT DATEPART(year, '2020/06/03 12:15:45') AS Output;

Output:

输出


2020


SQL Server DATEPART()方法,以“秒”为间隔 (SQL Server DATEPART() method with ‘second’ as an interval)

In the below example, the DATEPART() function is used to extract and return the ‘seconds’ value from the input “date” passed to it.

在下面的示例中,DATEPART()函数用于从传递给它的输入“日期”中提取并返回“秒”值。

Example:

例:


SELECT DATEPART(second, '2020/06/03 12:15:45') AS Output;

Output:

输出:


45


SQL DATEPART()方法,以“分钟”为间隔 (SQL DATEPART() method with ‘minute’ as an interval)

Here, the minute value of the date passed is returned by the function.

在此,该函数返回通过日期的分钟值。


SELECT DATEPART(minute, '2020/06/03 12:15:45') AS Output;

Output:

输出:


15


以“小时”为间隔的DATEPART()方法 (DATEPART() method with ‘hour’ as an interval)

If the time related information is passed along the date format to the function, we can extract the information about time such as hours, minutes, seconds, etc using the DATEPART() function.

如果将与时间相关的信息沿日期格式传递给该函数,则可以使用DATEPART()函数提取有关时间的信息,例如小时,分钟,秒等。

Example:

例:


SELECT DATEPART(hour, '2020/06/03 12:15:45') AS Output;

Output:

输出:


12


SQL Server DATEPART()方法,以“ dayofyear”为间隔 (SQL Server DATEPART() method with ‘dayofyear’ as an interval)

By passing ‘dayofyear’ to the function, we actually try to extract and estimate the day number from the passed year in the function.

通过将“ dayofyear”传递给函数,我们实际上尝试从函数中所传递的年份中提取和估计天数。

Example:

例:


SELECT DATEPART(dayofyear, '2020/06/03 12:15:45') AS Output;

Output:

出:


155


以“周”为间隔的DATEPART()方法 (DATEPART() method with ‘week’ as an interval)

Example:

例:


SELECT DATEPART(week, '2020/06/03 12:15:45') AS Output;

Output:

输出:


23


结论 (Conclusion)

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

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

Further, to gain access to such articles related to SQL Server, please do visit SQL Server JournalDev. Also, we’ve covered some more date functions in SQL like DATEADD() and DATEDIFF() which will be handy for you!

此外,要获得对此类与SQL Server有关的文章的访问权,请访问SQL Server JournalDev 。 另外,我们还介绍了SQL中的其他日期函数,例如DATEADD()DATEDIFF() ,它们对您来说很方便!



参考资料 (References)

翻译自: https://www.journaldev.com/40993/sql-server-datepart

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值