SQL Date Functions Overview
SQL Date Functions are special functions that are used to perform operations with dates and times. It helps to manipulate the date and time data types in SQL.
Syntax
<Function Name>(<Date or Time>)
Example
Let's take a look at some examples of SQL Date Functions:
SELECT CURDATE();
SELECT CURTIME();
SELECT NOW();
SELECT DATE_ADD('2022-01-01', INTERVAL 1 MONTH);
SELECT DATE_FORMAT('2022-01-01', '%W %M %Y');
Output
Function Name | Output |
---|---|
CURDATE() | 2022-07-04 |
CURTIME() | 10:30:00 |
NOW() | 2022-07-04 10:30:00 |
DATE_ADD('2022-01-01', INTERVAL 1 MONTH) | 2022-02-01 |
DATE_FORMAT('2022-01-01', '%W %M %Y') | Saturday January 2022 |
Explanation
- CURDATE() function returns the current date.
- CURTIME() function returns the current time.
- NOW() function returns the current date and time.
- DATE_ADD() function adds a specified value of time to a date.
- DATE_FORMAT() function formats a date in a specified format.
Use
SQL Date Functions are used for various purposes such as sorting dates, extracting only a part of the date, adding days, months or years to the date, etc.
Important Points
- SQL Date Functions are used to manipulate date and time data types.
- Use the appropriate date functions to get the desired output.
- Always use the correct format for date and time values.
Summary
SQL Date Functions are very useful for managing dates and times in a SQL database. They can be used to perform a variety of operations, such as adding or subtracting time, extracting the day of the week or month, formatting dates in a specific way, etc. By understanding SQL Date Functions, you can write powerful SQL queries that manipulate date information with ease.