Home Database Mysql Tutorial How to use the datediff function in SQL? (detailed code explanation)

How to use the datediff function in SQL? (detailed code explanation)

Mar 26, 2019 pm 01:46 PM
datediff sql

In SQL Server, you can use the T-SQL DATEDIFF() function to return the difference between two dates. It works with any expression that can be parsed into a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value. Therefore, you can also get the difference between the two times.

How to use the datediff function in SQL? (detailed code explanation)

This article provides examples of usage of the DATEDIFF() function in SQL Server.

DATEDIFF() function syntax is as follows:

DATEDIFF ( datepart , startdate , enddate )
Copy after login

where datepart is the part of the date you want to compare. startdate is the first date and enddate is the end date.

The way it works is by subtracting startdate from enddate.

Example 1

Here is a basic example where we can calculate the number of days between two dates:

SELECT DATEDIFF(day, '2001-01-01', '2002-01-01') AS Result;
Copy after login

Result:

+----------+
| Result   |
|----------|
| 365      |
+----------+
Copy after login

Example 2

Here is another example where I declared two variables and assigned them two different dates (I used DATEADD() to Add 1 year to the first date). Then use DATEDIFF() to return the individual dateparts for that date:

DECLARE @date1 datetime2 = '2000-01-01 00:00:00.0000000';
DECLARE @date2 datetime2 = DATEADD(year, 1, @date1);
SELECT 
    DATEDIFF( year, @date1, @date2 ) AS Years,
    DATEDIFF( quarter, @date1, @date2 ) AS Quarters,
    DATEDIFF( month, @date1, @date2 ) AS Months,
    DATEDIFF( week, @date1, @date2 ) AS Weeks,
    DATEDIFF( dayofyear, @date1, @date2 ) AS DayOfYear,
    DATEDIFF( day, @date1, @date2 ) AS Days;
Copy after login

Result:

+---------+------------+----------+---------+-------------+--------+
| Years   | Quarters   | Months   | Weeks   | DayOfYear   | Days   |
|---------+------------+----------+---------+-------------+--------|
| 1       | 4          | 12       | 53      | 366         | 366    |
+---------+------------+----------+---------+-------------+--------+
Copy after login

Example 3

As mentioned before, You can also return the time portion between dates. Here is an example of returning hours, minutes and seconds between date/time values:

DECLARE @date1 datetime2 = '2000-01-01 00:00:00.0000000';
DECLARE @date2 datetime2 = DATEADD(hour, 1, @date1);
SELECT 
    DATEDIFF( hour, @date1, @date2 ) AS Hours,
    DATEDIFF( minute, @date1, @date2 ) AS Minutes,
    DATEDIFF( second, @date1, @date2 ) AS Seconds;
Copy after login

Result:

+---------+-----------+-----------+
| Hours   | Minutes   | Seconds   |
|---------+-----------+-----------|
| 1       | 60        | 3600      |
+---------+-----------+-----------+
Copy after login

Example 4

The following is an example of getting the number of milliseconds, microseconds and nanoseconds between two date/time values:

DECLARE @date1 datetime2 = '2000-01-01 00:00:00.0000000';
DECLARE @date2 datetime2 = DATEADD(millisecond, 1, @date1);
SELECT    
    DATEDIFF( millisecond, @date1, @date2 ) AS Milliseconds,
    DATEDIFF( microsecond, @date1, @date2 ) AS Microseconds,
    DATEDIFF( nanosecond, @date1, @date2 ) AS Nanoseconds;
Copy after login

Result:

+----------------+----------------+---------------+
| Milliseconds   | Microseconds   | Nanoseconds   |
|----------------+----------------+---------------|
| 1              | 1000           | 1000000       |
+----------------+----------------+---------------+
Copy after login

Example 5 - Error!

If you try to do something extreme, like return the number of nanoseconds after 100 years, you'll get an error. This is because DATEDIFF() returns an int value, and there are more nanoseconds in 100 years than the int data type can handle.

What happens if you try this?

DECLARE @date1 datetime2 = '2000-01-01 00:00:00.0000000';
DECLARE @date2 datetime2 = DATEADD(year, 100, @date1);
SELECT    
    DATEDIFF( millisecond, @date1, @date2 ) AS Milliseconds,
    DATEDIFF( microsecond, @date1, @date2 ) AS Microseconds,
    DATEDIFF( nanosecond, @date1, @date2 ) AS Nanoseconds;
Copy after login

Result:

The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
Copy after login

Of course if you really have to find out how many nanoseconds there are in 100 years , then you can use the DATEDIFF_BIG() function. This function returns a signed bigint data type, which allows you to return larger values ​​than DATEDIFF().

Related recommendations: "MySQL Tutorial"

The above is the detailed content of How to use the datediff function in SQL? (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

What is the difference between HQL and SQL in Hibernate framework?

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

Usage of division operation in Oracle SQL

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What does the identity attribute in SQL mean?

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Comparison and differences of SQL syntax between Oracle and DB2

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? May 27, 2023 pm 01:10 PM

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations?

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

How to solve the 5120 error in SQL

How to use SQL statements for data aggregation and statistics in MySQL? How to use SQL statements for data aggregation and statistics in MySQL? Dec 17, 2023 am 08:41 AM

How to use SQL statements for data aggregation and statistics in MySQL?

See all articles