February 13, 2009 at 11:54 am
Hello,
I have a column which is integer data type and it represents number of seconds.
I need to convert it into MINUTES:SECONDS format.
Conversion of int into time is prohibited in SQL server.
MOD function doesn't seem to work in my SQL Server 2008 installation either
Could someone suggest how to do that?
Thanks,
Pit.
February 13, 2009 at 12:00 pm
SELECT
CONVERT(VARCHAR,[Column] / 60) + ':' + RIGHT('00' + CONVERT(VARCHAR,[Column] % 60),2)
FROM
[Table]
February 13, 2009 at 12:13 pm
declare @time int
set @time = 70
select cast((@time / 60) as varchar(2)) + ':' + cast((@time % 60) as varchar(2))
February 13, 2009 at 12:21 pm
Thanks a lot Michael!
It works fine.
Just curious, why MOD doesn't work in SQL 2008. Can I set some parameter to turn it on?
Thanks,
Pit.
February 13, 2009 at 12:28 pm
There is no MOD function in SQL Server; % is the modulus operator
select Mod_Test = 100 % 60, VER = left(@@version,60)
Results:
Mod_Test VER
----------- ------------------------------------------------------------
40 Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
(1 row(s) affected)
February 13, 2009 at 12:38 pm
DECLARE @seconds int;
SET @seconds = 14400;
SELECT DATEADD(second, @seconds, '20090101')
,CONVERT(char(5), DATEADD(second, @seconds, '20090101'), 108);
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
February 13, 2009 at 1:27 pm
Michael,
It's not I am arguing, but why SQL Server Online Help lists MOD function?
Pit.
February 13, 2009 at 2:55 pm
pshvets (2/13/2009)
Michael,It's not I am arguing, but why SQL Server Online Help lists MOD function?
Pit.
You sure you looking only at SQL server and not a visual basic reference as well?
Keep in mind that the MOD operator would work in SSIS or SSRS (since in some spots they use VB syntax and operators), but not in T-SQL
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
February 13, 2009 at 6:48 pm
pshvets (2/13/2009)
Hello,I have a column which is integer data type and it represents number of seconds.
I need to convert it into MINUTES:SECONDS format.
Conversion of int into time is prohibited in SQL server.
MOD function doesn't seem to work in my SQL Server 2008 installation either
Could someone suggest how to do that?
Thanks,
Pit.
Do you have an upper limit of 3599 seconds on this? What do you want to display if you hit 3600 seconds? What do you want to display if you hit 86400 seconds?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 15, 2009 at 1:56 pm
Jeff Moden (2/13/2009)
Do you have an upper limit of 3599 seconds on this? What do you want to display if you hit 3600 seconds? What do you want to display if you hit 86400 seconds?
[font="Verdana"]Those are the reasons I would recommend using the dateadd() approach (in another example above.)[/font]
February 15, 2009 at 7:32 pm
DateAdd or not, the op hasn't identified what happens when you exceed 59 minutes and 59 seconds. If the duration in seconds equals two days, should the answer still be expressed in minutes and seconds or not?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 16, 2009 at 11:22 am
I don't expect my data to exceed 20-30 min. I wouldn't need any special handling for such cases.
Thanks,
Pit.
February 16, 2009 at 12:36 pm
Then, the easiest way to handle things like leading zeros and the like, is to let SQL Server do it all for you...
SELECT RIGHT(CONVERT(CHAR(8),DATEADD(s,Seconds,0),108),5)
FROM (--==== Your table name would go here.... this is just for demo...
SELECT 1 AS Seconds UNION ALL
SELECT 10 UNION ALL
SELECT 100 UNION ALL
SELECT 1000 UNION ALL
SELECT 2000 UNION ALL
SELECT 3000 UNION ALL
SELECT 3599
) yourtable
Micheal Earl's method is probably better, though... if it does go over 3559 seconds, his will show minutes over 60. Mine is probably a bit faster because it has one less CONVERT, but it will not accomodate times over 59:59 and it will not warn you.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 15, 2009 at 9:36 am
So... did any of this solve your problem or not?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 15, 2009 at 10:16 am
Sorry for not posting back...
Yes, suggestion by Michael Earl had worked for me!
Thanks a lot for all help!
It is very usefull portal and forum.
Pit.
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply