Khrys
Khrys

Reputation: 2774

Convert Minutes in HH:MM in SQL-Server 2008R2

How can I get the result bellow:

SELECT CONVERT(VARCHAR(8),DateAdd(MINUTE, 8400, '00:00:00'),108)

Like: HH:MM? In this case it should return 140:00.

Thanks

Upvotes: 0

Views: 56

Answers (2)

Dhaval Asodariya
Dhaval Asodariya

Reputation: 558

Try this

Declare @Minutes int  = 398;
SELECT REPLACE(CONVERT(NUMERIC(18, 2), @Minutes / 60 + (@Minutes % 60) / 100.0),'.',':')

Output:

6:38

Upvotes: 0

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49270

You can use / and % to get the hours and minutes. Use replicate in case the % returns a value of length 1.

select cast(minutescol/60 as varchar(100)) + ':' + 
replicate('0', 2-len(minutescol%60)) + cast(minutescol%60 as varchar(100))

Upvotes: 2

Related Questions