本文共 4091 字,大约阅读时间需要 13 分钟。
Hey, folks! In this article, we will be focusing on SQL Server DATEPART() function.
嘿伙计! 在本文中,我们将重点介绍SQL Server DATEPART()函数 。
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 |
分钟 | , |
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,它是要从日期和输入时间戳中提取的部分。 该函数返回从中提取的日期部分
Now, let us understand the working behind the SQL Server DATEPART() function with the help of various examples stated below.
现在,让我们借助下面所述的各种示例来了解SQL Server DATEPART()函数背后的工作。
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
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
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
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
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
Example:
例:
SELECT DATEPART(week, '2020/06/03 12:15:45') AS Output;
Output:
输出:
23
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中的其他日期函数,例如和 ,它们对您来说很方便!
翻译自:
转载地址:http://yplzd.baihongyu.com/