Date functions are functions used to manipulate dates and times in SQL queries. Common date functions include getting the current date (CURDATE()), formatting the date (DATE_FORMAT()), adding or subtracting time intervals from the date (DATE_ADD(), DATE_SUB()), and extracting the time part (EXTRACT()) and converting strings to date values (STR_TO_DATE()). These functions are commonly used to process date-related data, such as getting the current date, formatting dates to meet specific needs, calculating the difference between dates, etc.
Date functions in SQL
What are date functions?
The date function is a function that operates on dates and times in SQL queries. They can be used to obtain, format, compare, and modify date and time values.
Common date functions
The following are some commonly used date functions in SQL:
Example of using date functions
Get the current date:
<code class="sql">SELECT CURDATE();</code>
Format the date:
<code class="sql">SELECT DATE_FORMAT('2023-03-08', '%Y-%m-%d');</code>
Add time interval to date:
<code class="sql">SELECT DATE_ADD('2023-03-08', INTERVAL 1 DAY);</code>
Subtract time interval from date:
<code class="sql">SELECT DATE_SUB('2023-03-08', INTERVAL 1 WEEK);</code>
Extract the time part of the date:
<code class="sql">SELECT EXTRACT(MONTH FROM '2023-03-08');</code>
Convert the string to a date value:
<code class="sql">SELECT STR_TO_DATE('03/08/2023', '%m/%d/%Y');</code>
The above is the detailed content of How to use date functions in sql. For more information, please follow other related articles on the PHP Chinese website!