0

I want to count total time in hh:mm:ss format. and I have minutes in int like (465).

I made:

CONVERT(varchar, CONVERT(datetime, cast(cast(TotalMin/60 as int) as nvarchar(50)) + ' : ' + cast(TotalMin%60 as nvarchar(50))),108) AS TotalMin

but it shows below error. Not in SQL Server but when I run code in c#:

Conversion failed when converting date and/or time from character string.

Akil M Belim
  • 115
  • 9

4 Answers4

1

You can use this code to get the string in SQL Server. It will convert hours and minutes and add 00 for seconds as you don't have them (you're using integer value for minutes):

declare @min int = 465

select right('0' + cast(@min/60 as varchar(2)),2)
    + ':'
    + right('0' + cast((@min - (@min/60)*60) as varchar(2)),2)
    + ':00'

It will work for up to 5999 minutes (99 hours and 59 minutes).


If you need a Unicode version:

declare @min int = 465

select right(N'0' + cast(@min/60 as nvarchar(2)),2)
    + N':'
    + right(N'0' + cast((@min - (@min/60)*60) as nvarchar(2)),2)
    + N':00'
Szymon
  • 42,577
  • 16
  • 96
  • 114
0

Try this:

TimeSpan t = TimeSpan.FromMinutes( TotalMin);

and see this for more

Community
  • 1
  • 1
Masoumeh Karvar
  • 791
  • 1
  • 14
  • 32
0
UPDATE MyTable SET MyDate = CONVERT(datetime, '2009/07/16 08:28:01', 120)

For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:

http://msdn.microsoft.com/en-us/library/ms187928.aspx

Muhammad
  • 1
  • 4
0

This will help you You want to multiply out to milliseconds as the fractional part is discarded.

SELECT DATEADD(ms, 121.25 * 1000, 0)

If you want it without the date portion you can use CONVERT, with style 114

SELECT CONVERT(VARCHAR, DATEADD(ms, 121.25 * 1000, 0), 114)
Kumod Singh
  • 2,113
  • 1
  • 16
  • 18