Collections:
DATEDIFF() - Calculating DATETIME Value Differences in SQL Server
How To Calculate DATETIME Value Differences Using the DATEDIFF() Function in SQL Server Transact-SQL?
✍: FYIcenter.com
If you want to calculate the difference between two date and time values, you can use the DATEDIFF() function in the following format:
DATEDIFF(datepart, startdate, enddate) returns INT: "startdate" - the start date "enddate" - the end date "datepart" - one of predefined date part names See other tutorials for the list of names
Note that SQL Server manual says DATEDIFF() returns the number of boundaries crossed by the specified part between two specified dates. This can be interpreted as truncating all parts smaller than the specified part on both input date and time values, then calculating the difference. The following tutorial exercise shows you some good DATEDIFF() usage examples:
-- Difference in days - truncating all parts -- smaller than day DECLARE @birth_date DATETIME; DECLARE @today_date DATETIME; SET @birth_date = '2000-02-29 16:10:00.000'; SET @today_date = '2000-03-02 18:30:00.000'; SELECT DATEDIFF(day, @birth_date, @today_date); GO 2 -- Difference in hours - truncating all parts -- smaller than hour DECLARE @birth_date DATETIME; DECLARE @today_date DATETIME; SET @birth_date = '2000-02-29 16:10:00.000'; SET @today_date = '2000-03-02 18:30:00.000'; SELECT DATEDIFF(hour, @birth_date, @today_date); GO 50
⇒ Calculating Age in Days, Hours and Minutes in SQL Server
⇐ DATEADD() Function Usage Examples in SQL Server
⇑ Date/Time Operations and Functions in SQL Server Transact-SQL
2017-02-20, 3146🔥, 0💬
Popular Posts:
How to execute statements in loops in SQL Server Transact-SQL? How to use WHILE ... loops? You can u...
How To Verify a User name with SQLCMD Tool in SQL Server? The quickest way to verify a user name in ...
Where to find answers to frequently asked questions on CREATE, ALTER and DROP Statements in MySQL? H...
Can You Drop an Index Associated with a Unique or Primary Key Constraint in Oracle? You can not dele...
What is SQL Server Transact-SQL (T-SQL)? SQL Server Transact-SQL, also called T-SQL, is an extension...