Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

How To Calculate How Many Minutes Have Passed Since Midnight With Datetime In SQL

From Wiki

Jump to: navigation, search

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


  1. DECLARE @d DATETIME
  2. SELECT @d = '2008-06-25 10:40:57.583'
  3.  
  4.  
  5. SELECT DATEDIFF(mi,CONVERT(VARCHAR(10),@d,112),@d)


There is another way, you can use datepart hour * 60 + datepart minute Take a look

  1. DECLARE @d DATETIME
  2. SELECT @d = '2008-06-25 10:40:57.583'
  3.  
  4.  
  5. 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

423 Rating: 1.6/5 (8 votes cast)