Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to count total time in hh:mm:ss format. and I have minutes in int like (465). I made:

SQL
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.


[edit]Code blocks fixed[/edit]
Posted
Updated 26-Dec-13 21:56pm
v3
Comments
Maciej Los 27-Dec-13 3:15am    
Please, post sample data...

Try
SQL
Select (CASE WHEN LEN(CONVERT(VARCHAR,465/60)) = 1 THEN ('0' + CONVERT(VARCHAR,465/60)) ELSE CONVERT(VARCHAR,465/60) END) + ':' + CONVERT(VARCHAR,465%60) + + ':00'


[edit]Code block added[/edit]
 
Share this answer
 
v3
Comments
akilmbelim 27-Dec-13 5:17am    
Friend Your code is really right but when I show this records to reports It shows like "0@0@0" It shows "@" in place of ":".
Convert your final output to time format or use below code


SQL
select convert(time,cast((465 / 60) as varchar(2)) + ':' + cast((465 % 60) as varchar(2)),108)


SQL
select replace(cast((465 % 1440) as varchar(2)),'*','0') + ':' +cast((465 / 60) as varchar(2)) + ':' + cast((465 % 60) as varchar(2))
 
Share this answer
 
v2
Comments
akilmbelim 26-Dec-13 5:02am    
Your code is right but it returns "1@38@00" with list, " : " in place of " @ "
Ganesh Raja 26-Dec-13 5:18am    
Are you sure?
After my testing only added this solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900