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.

1
  • Show us your C# code to run this query and get the results from it. Commented Dec 26, 2013 at 7:45

4 Answers 4

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' 
Sign up to request clarification or add additional context in comments.

1 Comment

Your code working but it shows "@" in place of ":" exa: 00@56@00
0

Try this:

TimeSpan t = TimeSpan.FromMinutes( TotalMin); 

and see this for more

1 Comment

I can not change Data type because I'm getting converting records from excel file.
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

Comments

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.