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 formatDATEPART()方法日期间隔格式


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)

  • SQL Server DATEPART() method — DocumentationSQL Server DATEPART()方法—文档

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

什么是SQL Server DATEPART()方法?相关推荐

  1. SQL Server灾难恢复方法

    两种SQL Server灾难恢复方法 如果您的mdf文件是当前数据库产生的,那么很侥幸,也许你使用sp_attach_db或者sp_attach_single_file_db可以恢复数据库,但是会出现 ...

  2. 干货 | SSMS客户端连接京东云RDS SQL Server配置方法

    干货 | SSMS客户端连接京东云RDS SQL Server配置方法 原创: 于振江 京东云开发者社区 微软SQL Server, Oracle数据库以及MySQL系列占据了关系型数据库市场的绝对份 ...

  3. mysql的datepart函数,SQL Server Datepart()函数

    在本教程中,将学习如何使用SQL Server DATEPART()函数来提取日期的一部分. SQL Server DATEPART()函数简介 DATEPART()函数返回一个整数,它是日期的一部分 ...

  4. sql server DATEPART() 函数的使用(注意防止入坑)

    datepart() datepart()函数是用来返回 指定日期时间的指定日期部分的整数.返回类型为:int. 语法: DATEPART(datepart,date) 其中date是日期.时间.pa ...

  5. sql server DATEPART() 函数的使用

    datepart() 函数是用来返回 指定日期时间的指定日期部分的整数.返回类型为:int .语法:DATEPART(datepart,date) 其中date是日期.时间.part指其中的一部分.所 ...

  6. .NET中SQL Server数据库连接方法

    1. 使用本机上的SQL Server Express 实例上的用户实例.       用户实例的连接创建了一个新的SQL Server 实例.此连接只能是在本地SQL Server 2005实例上并 ...

  7. MS SQL Server查询优化方法

    (1) ●查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4. ...

  8. Navicat——连接SQL Server数据库方法

    一.添加连接 SQL Server 验证 Windows 验证 二.错误:未发现数据源名称并且未发现默认驱动程序 有时候Navicat并没有初始化安装sqlncli, 所以连接的时候会报 没有默认驱动 ...

  9. 将数据插入SQL Server的方法

    摘要 (Summary) There are a variety of ways of managing data to insert into SQL Server. How we generate ...

最新文章

  1. express的cookie解析和签名源码解析
  2. 【Linux 内核】实时调度类 ⑦ ( 实时调度类核心函数源码分析 | dequeue_task_rt 函数 | 从执行队列中移除进程 )
  3. de casteljau算法_泊松分布算法的应用:开一家4S店
  4. Excel 常用函数 IF 用法
  5. java学习(21):移位运算符
  6. 使用python构建三层神经网络、softmax函数
  7. java+classpath的理解_Java配置path和classpath的含义详解-Go语言中文社区
  8. python如何保存源文件_python处理document文档保留原样式
  9. 钢构cad3.5免费版及命令索
  10. Asp.net 路由详解
  11. EAX、EBX、ECX、EDX、ESI、EDI、ESP、EBP 寄存器详解
  12. Windows环境下32位汇编语言程序设计(典藏版)(含CD光盘1张)
  13. c++调用python详细教程
  14. 方根法公式_(完整word版)层次分析法中用方根法计算权重在Excel中的具体操作
  15. Flutter架构图
  16. NSString中的rangeOfString
  17. 内容创作者的春天—web3.0时代
  18. Meth | Linux 修改所有者/用户组
  19. python学习-字符串的驻留机制
  20. layui自定义模块实现下拉框,读取数据字典数据

热门文章

  1. java io读书笔记(8)FileInputStream/FileOutputStream的应用
  2. 什么是BI(Business Intelligence
  3. 很累很失败,发奋学英语
  4. MATLAB 程序出现错误总结
  5. vss中项目与服务器断开绑定之后进行重新绑定得方法
  6. win10 64位 安装TensorFlow
  7. Unreal Engine 4 —— 基于Kajiya-Kay的材质迭代
  8. 手机页面操作栏的创建及WebFont的使用
  9. 无任何网络提供程序接受指定的网络路径
  10. 阿里云服务器的公网ip访问不到的问题