Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to convert second to minute and seconds in sql server 2005

Tags:

sql

tsql

Suppose I have 90 seconds. If I want to display the result in terms of minutes and second, I do it by using

select Time= '0' + CAST( 90/60 as varchar(2)) + ':' +  CAST( 90%60 as varchar(2)) 

The output is

Time
01:30

I have appended 0(zero) because if you do a select getdate() the output will be

yyyy-mm-dd hh:mm:ss:ms

What is the standard way and recommended practice to do such a conversion?

Thanks

like image 268
priyanka.bangalore Avatar asked Feb 23 '10 05:02

priyanka.bangalore


People also ask

How do you convert seconds to HH MM SS format in SQL?

SELECT SEC_TO_TIME( seconds ); SELECT SEC_TO_TIME( seconds ); In this query, the “seconds” parameter is the number of seconds to convert. The result will be displayed in “HH:MM:SS” format.


1 Answers

With hours:

SELECT CONVERT(CHAR(8),DATEADD(second,90,0),108)
00:01:30

Ignoring hours:

SELECT RIGHT(CONVERT(CHAR(8),DATEADD(second,90,0),108),5)
01:30
like image 152
Carlos Gutiérrez Avatar answered Sep 30 '22 13:09

Carlos Gutiérrez