SQL Server中的sql-server-2008 – UNIX_TIMESTAMP

前端之家收集整理的这篇文章主要介绍了SQL Server中的sql-server-2008 – UNIX_TIMESTAMP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在sql Server 2008中创建一个将会模仿MysqL的UNIX_TIMESTAMP()的函数.

提前致谢 !@H_301_3@

解决方法

试试这个帖子
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

或这篇文章:@H_301_3@

http://mysql.databases.aspfaq.com/how-do-i-convert-a-sql-server-datetime-value-to-a-unix-timestamp.html@H_301_3@

代码如下:@H_301_3@

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

样品用量:@H_301_3@

SELECT dbo.DTtoUnixTS(GETDATE()) 
-- or 
SELECT UnixTimestamp = dbo.DTtoUnixTS(someColumn) 
    FROM soMetable

猜你在找的MsSQL相关文章