博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
什么是SQL Server DATEPART()方法?
阅读量:2531 次
发布时间:2019-05-11

本文共 4091 字,大约阅读时间需要 13 分钟。

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_portion Format
year yy, yyyy
month mm, m
second ss, s
day dd, d
week wk, ww
weekday dw
hour hh
nanosecond ns
dayofyear dy, y
millisecond ms
microsecond mcs
minute mi, 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 . Also, we’ve covered some more date functions in SQL like and which will be handy for you!

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



参考资料 (References)

翻译自:

转载地址:http://yplzd.baihongyu.com/

你可能感兴趣的文章
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>