Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
How To Calculate How Many Minutes Have Passed Since Midnight With Datetime In SQL
From Wiki
If it is now June 25th 10:40 AM, how many minutes have elapsed since midnight?
Easy to figure out
(Hour * 60) + minutes
(10 * 60) + 40
So how do you do this in SQL?
You will have to get the date value, strip of the time and then use DATEDIFF to get the difference in minutes
Here is the code that will demonstrate this
- DECLARE @d DATETIME
- SELECT @d = '2008-06-25 10:40:57.583'
- SELECT DATEDIFF(mi,CONVERT(VARCHAR(10),@d,112),@d)
There is another way, you can use datepart hour * 60 + datepart minute
Take a look
- DECLARE @d DATETIME
- SELECT @d = '2008-06-25 10:40:57.583'
- SELECT (DATEPART(hh,@d) * 60) + DATEPART(mi,@d)
Both of the code blocks return 640
Contributed by: --SQLDenis 14:49, 25 June 2008 (GMT)
Part of SQL Server Programming Hacks
Section Dates



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.