Today, I came across this question in a forum – How to convert seconds to Day(s), Hour(s), Minute(s), Second(s)? For this we need to do a lot of divisions and remainders to convert Seconds to Day, Hour, Minute, Seconds. Given below is the simple script to convert it without doing any divisions and remainders,using only built-in SQL functions DateAdd, DatePart, DateDiff
--This script will work on SQL Server 2005 and above.Declare @Seconds as intDeclare @SubtractDate as datetime--Enter Number of Seconds hereSet @Seconds=9974501Set @SubtractDate=DateAdd(s,@Seconds,getdate()) - Getdate()Select Convert(varchar(10),DateDiff(day,'1900-01-01',@SubtractDate))+ ' Day(s) ' +Convert(varchar(10),DatePart(hh,@SubtractDate))+ ' Hour(s) ' +Convert(varchar(10),DatePart(mi,@SubtractDate))+ ' Minute(s) ' +Convert(varchar(10),DatePart(ss,@SubtractDate))+ ' Second(s) ' AS [Result] |
No comments:
Post a Comment