March 14, 2012 at 5:06 pm
hi,
Could you please give any formula for that to convert seconds to time...
Thanks,
Giri
March 14, 2012 at 6:42 pm
Yes sure ! ( A + B )2 = A2 + B2 + 2AB! 😀
On the other hand, convert to time = what?? time can be specified in hours, minutes, nano seconds, milli second et all.. what do u exactly want?
March 14, 2012 at 11:13 pm
dastagiri16 (3/14/2012)
hi,Could you please give any formula for that to convert seconds to time...
Thanks,
Giri
SELECT DATEADD(ss,someseconds,0)
That will give you a date/time and you probably won't like the looks of it but it's the first step. Will your seconds at up to less than 24 hours?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 15, 2012 at 12:09 am
What we are trying to say is that we need more information. To convert seconds to time we need to know the basis of the seconds. Is the number of seconds since midnight? From 1/1/1980?
March 15, 2012 at 5:11 am
Assuming you mean time as something like HH:MM:SS, this may work:
DECLARE @seconds INT
SELECT @seconds = 500
SELECT RIGHT('00'+CAST(@seconds/3600 AS VARCHAR), 2) + ':' +
RIGHT('00'+CAST(@seconds/60 AS VARCHAR), 2) + ':' +
RIGHT('00''00'+CAST(@seconds % 60 AS VARCHAR) , 2) AS [HH:MM:SS]
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
March 15, 2012 at 5:35 am
To get time as HH:MM:SS from seconds, you don't need to calculates hours, minutes and seconds, format and concatenate them separately. You can just use one of standard CONVERT options:
DECLARE @seconds INT
SELECT @seconds = 500
SELECT CONVERT(VARCHAR, DATEADD(second,@seconds,0),108)
March 15, 2012 at 7:01 am
Everyone is guessing at what you need. How about telling us what you really are trying to accomplish.
March 15, 2012 at 7:05 am
Sorry... post withdrawn... posted code for the wrong problem.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 15, 2012 at 6:38 pm
Rats! Eugene posted:
To get time as HH:MM:SS from seconds, you don't need to calculates hours, minutes and seconds, format and concatenate them separately. You can just use one of standard CONVERT options:
DECLARE @seconds INT
SELECT @seconds = 500
SELECT CONVERT(VARCHAR, DATEADD(second,@seconds,0),108)
I was wondering if CONVERT had a feature for this but I was too lazy to look it up!
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
March 24, 2012 at 3:47 pm
hi,
Thanks for replying ...
It is not working in all cases suppose the seconds is more than 100 hours its showing wrong .
Thanks,
Dastagiri.
March 24, 2012 at 4:01 pm
dastagiri16 (3/24/2012)
hi,Thanks for replying ...
It is not working in all cases suppose the seconds is more than 100 hours its showing wrong .
Thanks,
Dastagiri.
so...yo want elapsed time converted to years:months:hours:minutes:seconds, and not converted to datetime at all, then right?
Lowell
March 24, 2012 at 6:40 pm
Lowell (3/24/2012)
dastagiri16 (3/24/2012)
hi,Thanks for replying ...
It is not working in all cases suppose the seconds is more than 100 hours its showing wrong .
Thanks,
Dastagiri.
so...yo want elapsed time converted to years:months:hours:minutes:seconds, and not converted to datetime at all, then right?
Or do you just want hours, minutes, and seconds even if we go over 24 hours?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 25, 2012 at 10:15 am
Lowell,
could you please give me suggestion for..
if seconds=999999
then we can convert to time the result must be 277:46:39(hr:min:sec) ..
like that the earlier mail mentioned formula
DECLARE @seconds INT
SELECT @seconds = 500
SELECT RIGHT('00'+CAST(@seconds/3600 AS VARCHAR), 2) + ':' +
RIGHT('00'+CAST(@seconds/60 AS VARCHAR), 2) + ':' +
RIGHT('00''00'+CAST(@seconds % 60 AS VARCHAR) , 2) AS [HH:MM:SS]
this is work for that...
Thanks,
Dastagiri
March 25, 2012 at 11:34 am
dastagiri16 (3/25/2012)
Lowell,could you please give me suggestion for..
if seconds=999999
then we can convert to time the result must be 277:46:39(hr:min:sec) ..
like that the earlier mail mentioned formula
DECLARE @seconds INT
SELECT @seconds = 500
SELECT RIGHT('00'+CAST(@seconds/3600 AS VARCHAR), 2) + ':' +
RIGHT('00'+CAST(@seconds/60 AS VARCHAR), 2) + ':' +
RIGHT('00''00'+CAST(@seconds % 60 AS VARCHAR) , 2) AS [HH:MM:SS]
this is work for that...
Thanks,
Dastagiri
Now that we finally know which format you want the result to be, the problem solution becomes very simple if we let SQL Server do most of the work.
DECLARE @Seconds INT;
SELECT @Seconds = 999999;
SELECT CAST(@Seconds/3600 AS VARCHAR(10))
+ RIGHT(CONVERT(CHAR(8),DATEADD(ss,@Seconds,0),108),6);
Result:
----------------
277:46:39
(1 row(s) affected)
--Jeff Moden
Change is inevitable... Change for the better is not.
September 13, 2017 at 9:49 pm
dwain.c - Thursday, March 15, 2012 5:11 AMAssuming you mean time as something like HH:MM:SS, this may work:DECLARE @seconds INTSELECT @seconds = 500SELECT RIGHT('00'+CAST(@seconds/3600 AS VARCHAR), 2) + ':' + RIGHT('00'+CAST(@seconds/60 AS VARCHAR), 2) + ':' +RIGHT('00''00'+CAST(@seconds % 60 AS VARCHAR) , 2) AS [HH:MM:SS]
Will not work for negative dates and the datetime difference more than two digits in hour section
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply