In general I am very pleased with this answer. It took very little
tweaking to do exactly what I needed. I never even considered using
SQL's built in functions because when I first researched them, I
discovered that they consider the epoch to be 1900, so I didn't think
they would be useful. Silly me!
For posterity's sake, here is the final result code. I decided to use
user-defined functions instead of sprocs.
create function date2timestamp(
@dateStr datetime
)
returns bigint
as begin
-- Now, find out how many seconds have existed between now and the
epoch
return convert(bigint,
datediff(ss, '01-01-1970 00:00:00', @dateStr))
end
go
create function timestamp2date(
@numSeconds bigint
)
returns varchar(20)
as begin
-- set @numSeconds = @numSeconds + (60 * 60 * -5) -- subtract EST
-- Now, find out how many seconds have existed between @numSeconds and
the epoch
return dateadd(ss, @numSeconds, '01-01-1970 00:00:00')
end
go
|