create function [dbo].[HtoSec](@lvalue as int)
RETURNS int
BEGIN
DECLARE @temp int
Set @temp = @lvalue * 60 * 60
RETURN @temp
END
create function [dbo].[GetTime](@dtmValue as datetime)
RETURNS int
BEGIN
DECLARE @temp int
DECLARE @GMT_TIMEZONE int
SET @GMT_TIMEZONE = 8
Set @temp = DateDiff(s, cast('1970-01-01 00:00:00' as datetime), @dtmValue) - dbo.HToSEC(@GMT_TIMEZONE)
RETURN @temp
END
create function [dbo].[GetIntDate](@intValue as int)
RETURNS datetime
BEGIN
DECLARE @temp datetime
DECLARE @GMT_TIMEZONE int
SET @GMT_TIMEZONE = 8
Set @temp = DateAdd(s, @intValue + dbo.HToSEC(@GMT_TIMEZONE), cast('1970-01-01 00:00:00' as datetime))
RETURN @temp
END
另外:
在sql中将时间戳转换为时间类型
SQL里面有个DATEADD的函数。时间戳就是一个从1970-01-01 08:00:00到时间的相隔的秒数。所以只要把这个时间戳加上1970-01-01 08:00:00这个时间就可以得到你想要的时间了select DATEADD(second,1268738429 + 8 * 60 * 60,'1970-01-01 00:00:00')
注解:北京时间与GMT时间关系
1.GMT是中央时区,北京在东8区,相差8个小时
2.所以北京时间 = GMT时间 + 八小时
例如:
SELECT DATEADD(S,1160701488 + 8 * 3600,'1970-01-01 00:00:00') --时间戳转换成普通时间
SELECT DATEDIFF(S,'1970-01-01 00:00:00', '2006-10-13 09:04:48.000') - 8 * 3600 --普通时间转换成时间戳
****这个语句在sql2000中就能运行,在sql2005中运行总是提示错误?为什么?
oracle中时间戳的算法
获取时间戳:
create or replace function getTimeStamp return integer is
Result integer;
begin
SELECT (SYSDATE - TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24miss')) * 86400000
+ EXTRACT(SECOND FROM SYSTIMESTAMP(3)) * 1000
into result FROM DUAL;
return(Result);
end getTimeStamp;
时间戳变化为日期格式:
create or replace function getDateFromTimeStamp(tsp in integer) return date is
Result date;
tt integer;
begin
tt := substr(tsp, 0, 13);
SELECT ((tt - EXTRACT(SECOND FROM SYSTIMESTAMP(3)) * 1000) / 86400000 +
TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24miss'))
into result
FROM DUAL;
return(Result);
end getDateFromTimeStamp;