本文共 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!
所以,让我们开始吧!
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函数的必要性及其存在之后,现在让我们更加深入地了解同一个函数中的各种函数。
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.
有时,我们需要使用当前日期和时间在数据库和代码中进行操作。 以下功能集使用户能够从系统中提取并显示当前时间戳。
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
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
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
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
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日期函数包括以下命令/函数列表,这些命令/函数可以帮助我们根据日,月,年,时,分,秒,周,年,纳秒等提取日期或时间戳的一部分。
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
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
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
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
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
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函数包括以下一组函数,即对系统/输入时间戳值进行更改。
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
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
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 )构造或构建日期。
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
SQL Server TIMEFROMPARTS() function
builds time in the standard for from the input parameters as below–
SQL Server TIMEFROMPARTS() function
根据以下输入参数为标准构建时间–
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
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
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相关的更多此类主题,请访问 。
翻译自:
sql date 函数
转载地址:http://soqzd.baihongyu.com/