解决方法
试试这个帖子
http://skinn3r.wordpress.com/2009/01/26/t-sql-datetime-to-unix-timestamp/
http://skinn3r.wordpress.com/2009/01/26/t-sql-datetime-to-unix-timestamp/
CREATE FUNCTION UNIX_TIMESTAMP ( @ctimestamp datetime ) RETURNS integer AS BEGIN /* Function body */ declare @return integer SELECT @return = DATEDIFF(SECOND,{d '1970-01-01'},@ctimestamp) return @return END
或这篇文章:
代码如下:
CREATE FUNCTION dbo.DTtoUnixTS ( @dt DATETIME ) RETURNS BIGINT AS BEGIN DECLARE @diff BIGINT IF @dt >= '20380119' BEGIN SET @diff = CONVERT(BIGINT,DATEDIFF(S,'19700101','20380119')) + CONVERT(BIGINT,'20380119',@dt)) END ELSE SET @diff = DATEDIFF(S,@dt) RETURN @diff END
样品用量:
SELECT dbo.DTtoUnixTS(GETDATE()) -- or SELECT UnixTimestamp = dbo.DTtoUnixTS(someColumn) FROM soMetable