How to Subtract Dates in SQL Server (Years, Days, Hours, Seconds)
Автор: Coding With Brett
Загружено: 2022-03-10
Просмотров: 1753
Here's a quick video to show you how to use the DATEDIFF function to subtract dates from each other in SQL Server's transact SQL.
00:00 - Using DATEDIFF
00:30 - Calculating a person's age
01:27 - Subtracting variables
How to use DATEDIFF with column values:
SELECT JobTitle, BirthDate, DATEDIFF(year, BirthDate, GETDATE()) AS 'Rough Age in Years'
FROM [AdventureWorks].[HumanResources].[Employee]
Just be aware that calculating somebody's age is not as straightforward as it looks. DATEDIFF passing in the 'year' parameter just calculates whole years and does not take leap years into account. It's OK as a rough calculation but if you need to precisely calculate a person's age then check out this article:
https://www.wiseowl.co.uk/blog/s216/c...
If you want to calculate differences between past/present and future dates then you can use a query like this:
SELECT DATEDIFF(minute, GETDATE(), '2022-12-31 23:59:59') AS 'Total Minutes Left in Year'
DATEDIFF reference: https://docs.microsoft.com/en-us/sql/...
If you found this video leave a LIKE and SUBSCRIBE for more coding videos.
Check out my SQL Programming playlist for more tutorials: • SQL Programming
Thumbnail photo credit: https://unsplash.com/@mpho_mojapelo
Stock footage credits: https://www.pexels.com/@jack-nicole-1..., https://www.pexels.com/@cottonbro
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: