How to use Date Function?
How To Set Date Function In SQL
Introduction:
In this article, you will learn to work on Date function in SQL.
In the Set Date function, generally, we can use these types of datetime function:
- DATEADD()
- DATEDIFF()
- DATENAME()
- DATEPART()
- DAY()
- GETDATE()
- MONTH()
- YEAR()
DATEADD – DATEADD() function is used to add or subtract the date-time. After performing the operation, it gives a new date-time, based on add or subtract interval.
- Step 1: In this step, we will increase one month in our actual date.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will decrease one month in our actual date.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will increase one day in our actual date.
Now, select and execute this query.
Step 2: The output of this query is:
DATEDIFF- DATEDIFF() function is used to find the difference between the two days.
- Step 1: In this step, we will find the difference between the years.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will find the difference between the months.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will find the difference between the months.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will find the difference between the hours.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will find the difference between the minutes.
Now, select and execute this query.
Step 2: The output of this query is:
DATENAME - DATENAME() function is used to find the name with the help of datetime value.
- Step 1: In this step, we will show the month of the given date.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will show the day of the given date.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will show the year (yy) of the given date.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will show the hours of the given date and time.
Now, select and execute this query.
Step 2: The output of this query is:
- Step 1: In this step, we will show the minute of the given date.
Now, select and execute this query.
Step 2: The output of this query is:
DATEPART- DATEPART() function is used to divide the date or time. It has some queries, similarly to the DATENAME function.
DAY- Day() function is used to find the day from any datetime value.
- Step 1: In this step, we will find the day of the given date.
Now, select and execute this query.
Step 2: The output of this query is:
GETDATE - GATEDATE() function is used to return the date and time of the system. It does not contain any parameter.
- Step 1: In this step, we will find the current date.
Now, select and execute this query.
Step 2: The output of this query is:
MONTH()- MONTH() function is used to find the month from any DateTimelue.
- Step 1: In this step, we will find the current month.
Now, select and execute this query.
Step 2: The output of this query is:
YEAR - YEAR function is used to find the year from any datetime value.
- Step 1: In this step, we will find the current year.
Now, select and execute this query.
Step 2: The output of this query is:
Summary:
Thus, we learnt the use of datetime function in SQL.