博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql date 函数_SQL Server DATE函数–终极指南
阅读量:2533 次
发布时间:2019-05-11

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

sql date 函数

Hey, folks! In this article, we will be focusing on the entire set of SQL Server Date Functions in detail. We’ve already learned about the , , and other functions.

嘿伙计! 在本文中,我们将重点关注整个SQL Server日期函数集。 我们已经了解了 , 和其他函数。

So, let’s get started!

所以,让我们开始吧!



需要SQL Server日期功能 (Need of SQL Server Date Functions)

While manipulating or analyzing a certain form of data stored or collected from various sources, the most important aspect is the ‘Timestamp’ i.e. the date and time.

在处理或分析从各种来源存储或收集的某种形式的数据时,最重要的方面是“时间戳”,即日期和时间。

SQL Server Date functions are a set of functions that helps in extracting, manipulating, and analyzing the recorded or system date and time.

SQL Server日期函数是一组有助于提取,处理和分析记录的或系统的日期和时间的函数。

Having understood the necessity of SQL Server Date functions and its existence, let us now dive even more deeply to understand various functions within the same.

了解了SQL Server Date函数的必要性及其存在之后,现在让我们更加深入地了解同一个函数中的各种函数。



提取当前日期和时间 (Extracting the current date and time)

At times, we require to use the current date and time for manipulation within our database and code. The following set of functions enables the user to extract and display the current timestamp from the system.

有时,我们需要使用当前日期和时间在数据库和代码中进行操作。 以下功能集使用户能够从系统中提取并显示当前时间戳。

  • GETDATE() function

    GETDATE()函数
  • CURRENT_TIMESTAMP() function

    CURRENT_TIMESTAMP()函数
  • GETUTCDATE() function

    GETUTCDATE()函数
  • SYSDATETIME() function

    SYSDATETIME()函数


1. GETDATE()函数 (1. GETDATE() function)

accepts no parameter and returns the current date and time on which the SQL Server is running at that point in time.

接受任何参数,并返回该时间点SQL Server运行的当前日期和时间。

Syntax:

句法:

GETDATE()

Example:

例:

select GETDATE();

Output:

输出:

2020-06-05T16:52:40.46Z


2. CURRENT_TIMESTAMP()函数 (2. CURRENT_TIMESTAMP() function)

SQL Server CURRENT_TIMESTAMP() function accepts no parameters and returns the current timestamp without taking the timezone into consideration for the output.

SQL Server CURRENT_TIMESTAMP() function接受任何参数,并且在不考虑输出时区的情况下返回当前时间戳。

Syntax:

句法:

CURRENT_TIMESTAMP

Example:

例:

select CURRENT_TIMESTAMP as Date;

Output:

输出:

Date2020-06-05T16:54:21.477Z


3. GETUTCDATE()函数 (3. GETUTCDATE() function)

SQL Server GETUTCDATE() function too accepts no parameters and returns the current UTC timestamp as an integer value.

SQL Server GETUTCDATE() function也不接受任何参数,并以整数值返回当前UTC时间戳。

Syntax:

句法:

GETUTCDATE()

Example:

例:

select GETUTCDATE();

Output:

输出:

2020-06-07T09:34:56.47Z


4. SYSDATETIME()函数 (4. SYSDATETIME() function)

SQL Server SYSDATETIME() function returns the current date and time with more precision in terms of miliseconds, etc.

SQL Server SYSDATETIME() function以毫秒为单位,以更精确的方式返回当前日期和时间。

Syntax:

句法:

SYSDATETIME()

Example:

例:

select SYSDATETIME();

Output:

输出:

2020-06-07 09:37:44.6698077


提取日期和时间的一部分 (Extracting a portion of the date and time)

SQL Server Date functions include the below list of commands/functions that helps us extract a part of the date or timestamp in terms of the day, month, year, hour, minute, second, week, dayofyear, nanoseconds and so on.

SQL Server日期函数包括以下命令/函数列表,这些命令/函数可以帮助我们根据日,月,年,时,分,秒,周,年,纳秒等提取日期或时间戳的一部分。

  • DATENAME() function

    DATENAME()函数
  • DAY() function

    DAY()函数
  • MONTH() function

    MONTH()函数
  • YEAR() function

    YEAR()函数
  • DATEPART() funtion

    DATEPART()函数


1. DATENAME()函数 (1. DATENAME() function)

SQL Server DATENAME() function helps to extract and display a portion of date in terms of the year, month, day, etc as a character string.

SQL Server DATENAME() function有助于提取和显示年,月,日等日期的一部分字符串。

That is, the DATENAME() function returns the extracted portion of the date as a character value.

也就是说,DATENAME()函数返回日期的提取部分作为字符值。

Syntax:

句法:

DATENAME(date_portion,input_date)

Example:

例:

select DATENAME(year,'2020/06/16');

In the above example, we have extracted ‘year’ value and represented it as a character string from the input date value.

在上面的示例中,我们提取了“年”值并将其表示为输入日期值中的字符串。

Output:

输出:

2020


2. DAY()函数 (2. DAY() function)

SQL Server DAY() function accepts the date as input, extracts the day value from the input and returns the day value as an integer.

SQL Server DAY() function接受日期作为输入,从输入中提取日期值,并以整数形式返回日期值。

Syntax:

句法:

DAY(input_date)

Example:

例:

select DAY('2020/06/16');

Output:

输出:

16


3. MONTH()函数 (3. MONTH() function)

SQL Server MONTH() function accepts the date as input, extracts the month value from the input and returns it as an integer.

SQL Server MONTH() function接受日期作为输入,从输入中提取月份值,然后将其作为整数返回。

Syntax:

句法:

MONTH(input_date)

Example:

例:

select MONTH('2020/06/16');

Output:

输出:

6


4. YEAR()函数 (4. YEAR() function)

SQL Server YEAR() function accepts the date as input, extracts the year value from the input and returns the year value as an integer.

SQL Server YEAR() function接受日期作为输入,从输入中提取年份值,并以整数形式返回年份值。

Syntax:

句法:

YEAR(input_date)

Example:

例:

select YEAR('2020/06/16');

Output:

输出:

2020


5. DATEPART()函数 (5. DATEPART() function)

is very similar to DATENAME() function with a single point of difference.

DATENAME() function非常相似,只是有一点不同。

The DATEPART() function accepts the date portion to be extracted and the input date. It returns the extracted date portion as an integer value unlike DATENAME() function which returns the extracted portion as a character string.

DATEPART() function接受要提取的日期部分和输入日期。 它不同于DATENAME()函数,将提取的日期部分作为整数返回,而DATENAME()函数将提取的日期部分作为字符串返回。

Syntax:

句法:

DATEPART(date_portion,input_date)

Example:

例:

select DATEPART(month,'2020/06/16');

Output:

输出:

6


操纵日期和时间 (Manipulation of date and time)

SQL Server Date functions include the following set of functions to manipulate i.e. make changes to the system/input timestamp values.

SQL Server Date函数包括以下一组函数,即对系统/输入时间戳值进行更改。

  • DATEDIFF() function

    DATEDIFF()函数


1. DATEADD()函数 (1. DATEADD() function)

SQL Server DATEADD() function helps us add an integer value to the date_portion of the input date passed to it.

SQL Server DATEADD() function可帮助我们在传递给它的输入日期的date_portion中添加一个整数值。

For more details about DATEADD() function, please do visit .

有关DATEADD()函数的更多详细信息,请访问 。

Syntax:

句法:

DATEADD(date_portion,value,inout_date)

Example:

例:

select DATEADD(day,14,'2020/06/16');

Output:

输出:

2020-06-30T00:00:00Z


2. DATEDIFF()函数 (2. DATEDIFF() function)

is used to return the difference between the date_portions of two input date values passed to it.

用于返回传递给它的两个输入日期值的date_portions之差。

Syntax:

句法:

DATEDIFF(date_portion,input_date1,input_date2)

Example:

例:

select DATEDIFF(month,'2020/01/12','2020/06/16');

Output:

输出:

5


构造日期和时间 (Constructing date and time )

SQL Server Date functions include some set of functions that help us construct or build date in a standard format(yyyy-mm-dd) from the data provided to the function.

SQL Server日期函数包括一些函数集,这些函数集可帮助我们从提供给函数的数据中以标准格式( yyyy-mm-dd )构造或构建日期。

  • DATEFROMPARTS() function

    DATEFROMPARTS()函数
  • TIMEFROMPARTS() function

    TIMEFROMPARTS()函数


1. DATEFROMPARTS()函数 (1. DATEFROMPARTS() function)

SQL Server DATEFROMPARTS() function is used to construct a date from the various date portions provided as parameters.

SQL Server DATEFROMPARTS() function用于从作为参数提供的各种日期部分构造日期。

Syntax:

句法:

DATEFROMPARTS(year,month,day)

Example:

例:

select DATEFROMPARTS(2020, 06, 14);

Output:

输出:

2020-06-14


2. TIMEFROMPARTS()函数 (2. TIMEFROMPARTS() function)

SQL Server TIMEFROMPARTS() function builds time in the standard for from the input parameters as below–

SQL Server TIMEFROMPARTS() function根据以下输入参数为标准构建时间–

  • hour

    小时
  • minute

    分钟
  • second

    第二
  • fraction

    分数
  • precision value

    精度值

Note: The precision value can never be NULL.

注意:精度值永远不能为NULL。

Syntax:

句法:

TIMEFROMPARTS(hour,minute,second,fraction,precision)

Example:

例:

SELECT TIMEFROMPARTS(7, 25, 33, 0, 0);

Output:

输出:

07:25:33


确认日期和时间 (Validation of date and time)

In SQL Server, the valid format of date is ‘yyyy-mm-dd‘. SQL Server ISDATE() function is used to check whether the entered date follows the standard format or not.

在SQL Server中,日期的有效格式为' yyyy-mm-dd '。 SQL Server ISDATE()函数用于检查输入的日期是否遵循标准格式。

Syntax:

句法:

ISDATE(input_date)

The ISDATE() function returns 1, if the date follows the standard format. If the format of date differs, it returns 0.

如果日期遵循标准格式,则ISDATE() function将返回1。 如果日期格式不同,则返回0。

Example 1:

范例1:

SELECT ISDATE('2020-06-14') AS Valid_Date

Output:

输出:

Valid_date1

Example 2:

范例2:

SELECT ISDATE('2020-14-06') AS Valid_Date

Output:

输出:

Valid_Date0


结论 (Conclusion)

By this, we have come to the end of this topic. Please feel free to comment below, incase you come across any doubt.

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

For more such topics related to SQL Server, please do visit .

有关与SQL Server相关的更多此类主题,请访问 。



参考资料 (References)

翻译自:

sql date 函数

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

你可能感兴趣的文章
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>