SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to convert INTEGER to TIME


How to convert INTEGER to TIME

Author
Message
pshvets
pshvets
Old Hand
Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)

Group: General Forum Members
Points: 380 Visits: 154
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.
Michael Earl-395764
Michael Earl-395764
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12431 Visits: 23078
SELECT
CONVERT(VARCHAR,[Column] / 60) + ':' + RIGHT('00' + CONVERT(VARCHAR,[Column] % 60),2)
FROM
[Table]
gyessql
gyessql
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 898


declare @time int
set @time = 70

select cast((@time / 60) as varchar(2)) + ':' + cast((@time % 60) as varchar(2))



pshvets
pshvets
Old Hand
Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)

Group: General Forum Members
Points: 380 Visits: 154
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.
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13707 Visits: 11848
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)



Jeffrey Williams 3188
Jeffrey Williams 3188
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18023 Visits: 10039
DECLARE @seconds int;
SET @seconds = 14400;

SELECT DATEADD(second, @seconds, '20090101')
,CONVERT(char(5), DATEADD(second, @seconds, '20090101'), 108);

Jeffrey Williams
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

pshvets
pshvets
Old Hand
Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)

Group: General Forum Members
Points: 380 Visits: 154
Michael,
It's not I am arguing, but why SQL Server Online Help lists MOD function?

Pit.
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27894 Visits: 18995
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?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203718 Visits: 41949
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Bruce W Cassidy
Bruce W Cassidy
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2949 Visits: 1033
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?

Those are the reasons I would recommend using the dateadd() approach (in another example above.)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search