t–sql pl–sql
Hey, folks! Hope you all are doing well. Today, in this article, we will be highlighting some of the important SQL DATE TIME functions.
嘿伙计! 希望大家一切都好。 今天,在本文中,我们将重点介绍一些重要的SQL DATE TIME函数 。
什么是SQL日期时间函数? (What are the SQL Date Time functions?)
SQL Date Time functions are a set of functions specifically defined for manipulating and dealing with datetime values. These functions help us to segregate and work with the datetime values efficiently.
SQL日期时间函数是专门为处理和处理日期时间值而定义的一组函数。 这些功能有助于我们有效地隔离和使用日期时间值。
So, let us get started!
所以,让我们开始吧!
1. ADDDATE()函数 (1. ADDDATE() function)
The ADDDATE() function
enables us to add a particular expression to the specified datetime value.
ADDDATE() function
使我们能够将特定表达式添加到指定的datetime值。
Syntax:
句法:
ADDDATE(date, INTERVAL expression unit)
Thus, by using ADDDATE() function, we can add various expressions to it such as —
因此,通过使用ADDDATE()函数,我们可以向其中添加各种表达式,例如-
- second 第二
- minute 分钟
- hour 小时
- day 天
- month 月
- week 周
- year 年
- quarter, etc. 季度等
The ADDDATE() function returns a datetime expression after addition of the particular expression to it.
ADDDATE()函数在添加了特定表达式之后返回一个日期时间表达式。
Let us try to implement the same through an example:
让我们尝试通过一个示例实现相同的功能:
SELECT ADDDATE('2000-02-01', INTERVAL 5 Month) as add_month;
SELECT ADDDATE('2000-02-01', INTERVAL 25 Minute) as add_minute;
Here, we have added the month and minute expression values to the date value of the function.
在这里,我们将月份和分钟表达式值添加到函数的日期值中。
Output:
输出:
add_month
2000-07-01
add_minute
2000-02-01 00:25:00
2. ADDTIME()函数 (2. ADDTIME() function)
The ADDTIME() function
adds a datetime expression with another expression and returns the updated datetime value.
ADDTIME() function
将日期时间表达式与另一个表达式相加,并返回更新的日期时间值。
Syntax:
句法:
ADDTIME(expression1, expression2)
Consider the below example,
考虑下面的示例,
SELECT ADDTIME('2000-07-12 11:45:15','5 2:14:14') as updated_timestamp;
In this example, we have added day and time values to the give date time expression.
在此示例中,我们将日期和时间值添加到给定日期时间表达式中。
Output:
输出:
updated_timestamp
2000-07-17 13:59:29
3. CURRENT_DATE()函数 (3. CURRENT_DATE() function)
The CURRENT_DATE() function
extracts the current date expression from the system and returns it in the following format–
CURRENT_DATE() function
从系统中提取当前日期表达式,并以以下格式返回它–
YYYY-MM-DD
YYYY-MM-DD
Syntax:
句法:
CURRENT_DATE;
This function does not take any argument as input and returns the date expression.
此函数不接受任何参数作为输入,而是返回日期表达式。
SELECT CURRENT_DATE;
Output:
输出:
CURRENT_DATE
2020-07-15
4. CURRENT_TIME()函数 (4. CURRENT_TIME() function)
The CURRENT_TIME() function extracts and returns the current time expression from the underlying system.
CURRENT_TIME()函数从基础系统中提取并返回当前时间表达式。
Syntax:
语法 :
CURRENT_TIME;
This function returns the time expression in the form of HH–MM–SS.
此函数以HH–MM–SS的形式返回时间表达式。
Example:
例:
SELECT CURRENT_TIME;
Output:
输出:
CURRENT_TIME
11:27:12
5. CURRENT_TIMESTAMP()函数 (5. CURRENT_TIMESTAMP() function)
The CURRENT_TIMESTAMP() function
returns the current date-time expression from the system in the following format —
CURRENT_TIMESTAMP() function
以下列格式从系统返回当前日期时间表达式:
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS
Syntax:
句法:
CURRENT_TIMESTAMP;
Let us have a look at the below example to understand the working of the above function.
让我们看下面的示例,以了解上述功能的工作。
SELECT CURRENT_TIMESTAMP;
Output:
输出:
CURRENT_TIMESTAMP
2020-07-15T11:34:21Z
6. DATE()函数 (6. DATE() function)
The DATE() function extracts the date expression from the passed timestamp value and returns the date.
DATE()函数从传递的时间戳值中提取日期表达式并返回日期。
Syntax:
句法:
DATE(timestamp);
In the below example, we have extracted the date expressions from the timestamp values passed to the function.
在下面的示例中,我们从传递给函数的时间戳值中提取了日期表达式。
SELECT DATE('2020-04-04 23:34:34') as DATE;
SELECT DATE(CURRENT_TIMESTAMP) as DATE;
Output:
输出:
DATE
2020-04-04
DATE
2020-07-15
7. DATE_SUB()函数 (7. DATE_SUB() function)
The DATE_SUB() function
subtracts a particular expression from the specified date expression.
DATE_SUB() function
从指定的日期表达式中减去一个特定的表达式。
Syntax:
句法:
DATE_SUB(date, INTERVAL expression-value unit)
Let us try to implement the same through an example.
让我们尝试通过一个示例来实现相同的功能。
SELECT DATE_SUB('2020-12-12', INTERVAL 5 MONTH)
Output:
输出:
DATE_SUB('2020-12-12', INTERVAL 5 MONTH)
2020-07-12
8. EXTRACT()函数 (8. EXTRACT() function)
The EXTRACT() function
extracts a particular expression from the date value and returns the expression.
EXTRACT() function
从日期值中提取特定表达式并返回该表达式。
Syntax:
句法:
EXTRACT(expression from date);
In the below example, we have extracted the month value from the date expression passed to the function.
在下面的示例中,我们从传递给函数的日期表达式中提取了月份值。
SELECT EXTRACT(MONTH from '2020-5-12')
Output:
输出:
EXTRACT(MONTH from '2020-5-12')
5
9. NOW()函数 (9. NOW() function)
The NOW() function
returns the current timestamp from the system.
NOW() function
从系统返回当前时间戳。
Syntax:
句法:
NOW();
Example:
范例 :
SELECT NOW() as Present_Time;
Output:
输出:
Present_Time
2020-07-15T12:17:08Z
10. QUARTER()函数 (10. QUARTER() function)
The QUARTER() function
returns the quarter value of the year from the date expression passed to it.
QUARTER() function
从传递给它的日期表达式返回年份的四分之一值。
Syntax:
句法:
SELECT QUARTER(NOW()) as Quarter_Year;
Here the NOW() function returns 2020-07-15T12:17:08Z as the timestamp value from which the quarter value based upon the month as described below–
此处的NOW()函数返回2020-07-15T12:17:08Z作为时间戳值,从该时间戳值开始,如下所述,基于月份的四分之一值–
- 1-3: 1st Quarter 1-3:第一季度
- 4-6: 2nd Quarter 4-6:第二季度
- 7-9: 3rd Quarter 7-9:第三季度
- 10-12: 4th Quarter 10-12:第四季度
Output:
输出:
Quarter_Year
3
11. TIME()函数 (11. TIME() function)
The Time() function
extracts the time expression from the datetime expression and returns the time value.
Time() function
从datetime表达式中提取时间表达式并返回时间值。
Syntax:
句法:
TIME(datetime)
Example:
例:
SELECT TIME(NOW());
Output:
输出:
TIME(NOW())
07:34:38
12. TIMESTAMP()函数 (12. TIMESTAMP() function)
The TIMESTAMP() function
takes date expression as an argument and adds the time expression to it. Thus, the function returns a datetime expression.
TIMESTAMP() function
将日期表达式作为参数,并将时间表达式添加到其中。 因此,该函数返回日期时间表达式。
Syntax:
句法:
TIMESTAMP(date)
Example:
例:
SELECT TIMESTAMP('2020-11-11');
Output:
输出:
TIMESTAMP('2020-11-11')
2020-11-11T00:00:00Z
13. TIMEDIFF()函数 (13. TIMEDIFF() function)
The TIMEDIFF() function
returns the difference between two-time expressions.
TIMEDIFF() function
返回两次表达式之间的差。
Syntax:
句法:
TIMEDIFF(time1, time2)
Example:
例:
SELECT TIMEDIFF('30:12:20','20:10:10');
Output:
输出:
TIMEDIFF('30:12:20','20:10:10')
10:02:10
14. TO_DAYS()函数 (14. TO_DAYS() function)
The TO_DAYS() function
returns the count of days between 0 and the date expression passed to the function.
TO_DAYS() function
返回0到传递给该函数的日期表达式之间的天数。
Syntax:
句法:
TO_DAYS(date)
Example:
例:
SELECT TO_DAYS('2020-10-10')
Output:
输出:
TO_DAYS('2020-10-10')
738073
15.提取日期时间表达式的一部分 (15. Extracting portion of datetime expressions)
We can extract the year, month, hour, seconds value, etc from the datetime expressions as shown below–
我们可以从日期时间表达式中提取年,月,时,秒等值,如下所示–
SELECT YEAR('2020-10-10 12:30:30') as Year;
SELECT MONTH('2020-10-10 12:30:30') as Month;
SELECT MINUTE('2020-10-10 12:30:30') as Minute;
SELECT WEEK('2020-10-10 12:30:30') as WEEK;
Output:
输出:
Year
2020
Month
10
Minute
30
WEEK
40
结论 (Conclusion)
By this, we have come to the end of this topic. Feel free to comment below in case you come across any question.
至此,我们到了本主题的结尾。 如果您遇到任何问题,请在下面发表评论。
For more such posts related to SQL, do visit SQL JournalDev.
有关与SQL有关的更多此类帖子,请访问SQL JournalDev 。
参考资料 (References)
t–sql pl–sql