December 13, 2005 at 11:47 am
Hi, another basic question as you are really good and quick 🙂
I have a field, a time field, containing the number of seconds.
I would like to convert it, easly to a number of day, hours, minutes and seconds ...
Cheers,
December 13, 2005 at 12:21 pm
look at the datepart function.
datepart(hh,mydate)
will give you an integer of the hour.
December 14, 2005 at 3:11 am
Well, doesn't seem to work fine.
I've got an 'Arithmetic overflow error converting expression to data type datetime'
I have a value for exemple: 508621 (seconds)
I want to display: 5 days 21 hour 17 mn 1 sec
December 14, 2005 at 3:35 am
DECLARE @date datetime
SET @date = DATEADD(second,508621,0)
SELECT
CAST(DATEPART(dayofyear,@date)-1 as varchar) + ' days ' +
CAST(DATEPART(hour,@date) as varchar) + ' hour ' +
CAST(DATEPART(minute,@date) as varchar) + ' mn ' +
CAST(DATEPART(second,@date) as varchar) + ' sec'
Far away is close at hand in the images of elsewhere.
Anon.
December 14, 2005 at 4:06 am
Ok, one more question because I m getting an error ...
My field, TS_TIME_ASSIGNED is an integer
If I replace the '508621' by my field TS_TIME_ASSIGNED in the second line (SET @date = DATEADD(second,TS_TIME_ASSIGNED,0)), I have the following error:
Server: Msg207, Level 61, State 3, Line 2
Invalid column name 'TS_TIME_ASSIGNED
But the name is correct ...
December 14, 2005 at 4:33 am
OK try this, may work better as no conversion to datetime required
SELECT
CAST(TS_TIME_ASSIGNED / 86400 as varchar) + ' days ' +
CAST((TS_TIME_ASSIGNED % 86400) / 3600 as varchar) + ' hour ' +
CAST(((TS_TIME_ASSIGNED % 86400) % 3600) / 60 as varchar) + ' mn ' +
CAST(((TS_TIME_ASSIGNED % 86400) % 3600) % 60 as varchar) + ' sec'
FROM
Far away is close at hand in the images of elsewhere.
Anon.
December 14, 2005 at 8:35 am
You might try using a function like this:
CREATE FUNCTION fn_GetDays(@InSecs As int)
RETURNS varchar(36) AS
BEGIN
declare
@GetDays varChar(200)
-- Get the Days, Hours, Minutes, Seconds returned with input in Seconds
-- Use: Select traffic.dbo.fn_Getdays(96453) as Days
declare Cursor1 cursor for
Select
CAST(@InSecs / 86400 as varchar) + ' days ' +
CAST((@InSecs % 86400) / 3600 as varchar) + ' hours ' +
CAST(((@InSecs % 86400) % 3600) / 60 as varchar) + ' mins ' +
CAST(((@InSecs % 86400) % 3600) % 60 as varchar) + ' secs'
open cursor1
fetch next from cursor1 into @getdays
close cursor1
deallocate cursor1
return @GetDays
END
December 14, 2005 at 8:42 am
Don't need to use a cursor
CREATE FUNCTION fn_GetDays(@Secs As int)
RETURNS varchar(36) AS
BEGIN
DECLARE @GetDays varchar(36)
-- Get the Days, Hours, Minutes, Seconds returned with input in Seconds
-- Use: Select .dbo.fn_GetDays(96453) as Days
SET @GetDays =
CAST(@Secs / 86400 as varchar) + ' days ' +
CAST((@Secs % 86400) / 3600 as varchar) + ' hours ' +
CAST(((@Secs % 86400) % 3600) / 60 as varchar) + ' mins ' +
CAST(((@Secs % 86400) % 3600) % 60 as varchar) + ' secs'
RETURN @GetDays
END
Far away is close at hand in the images of elsewhere.
Anon.
December 14, 2005 at 9:02 am
Thank you David. Trying to stay away from cursors. Old habits die hard!!
December 14, 2005 at 11:08 pm
You certainly don't need a cursor and, if you don't want, you don't need a function... "Trust the force, Luke."
SELECT CONVERT(VARCHAR(10),SecondsCol/86400)+':'
+ CONVERT(VARCHAR(8),DATEADD(ss,SecondsCol,0),108)
FROM yourtable
The forces to trust are:
1. Integer math
2. Instrinsic datetime formats
--Jeff Moden
Change is inevitable... Change for the better is not.
December 15, 2005 at 8:22 am
Pretty nifty Jeff. Thanks for the coaching. I use functions a lot because I hate to remember and then type all the syntax!!
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply