Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime to totalminute in sql

How can I get the total minute for sql datetime?

Let's say:

select getdate() from table

In this way, I will get everything, but I only want to get total minute. For eg, if the time is 07:10:35, I want 430.

How to achieve that?

The value from the field is 01-01-2001 07:10:40 The result I want is 430 ((7*60)+10) only.

like image 951
william Avatar asked Dec 27 '10 02:12

william


People also ask

How do I convert a date to a string in SQL?

In order to convert a DateTime to a string, we can use CONVERT() and CAST() function. These functions are used to converts a value(of any datatype) into a specified datatype.

How do I convert a datetime to date in SQL?

You can convert a DATETIME to a DATE using the CONVERT function. The syntax for this is CONVERT (datetime, format). This shows the date only and no time.


2 Answers

(DATEPART(HOUR,GETDATE()) * 60) + DATEPART(MINUTE,GETDATE())
like image 110
David Cely Avatar answered Sep 18 '22 16:09

David Cely


Here's a sample:

DECLARE @dt datetime 
SET @dt = '01-01-2001 07:10:20'
SELECT DATEDIFF(MINUTE, DATEADD(DAY, DATEDIFF(DAY, 0, @dt), 0), @dt)
like image 28
Stuart Ainsworth Avatar answered Sep 18 '22 16:09

Stuart Ainsworth