项目中使用BIGINT来存放时间,以下代码用来转换时间类型和BIGINT类型
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: GGA
-- Create date: 2013-03-28
-- Description: 将时间类型转化成BIGINT,返回指定时间
-- 到-01-01 08:00:00.000的毫秒数
-- =============================================
CREATE FUNCTION [dbo].[ufn_GetMSIntFormTime]
(
@dt DATETIME
)
RETURNS BIGINT
AS
BEGIN
-- Declare the return variable here
DECLARE @dtInt BIGINT
SELECT @dtInt=DATEDIFF(SECOND , '1970-01-01 08:00:00.000' , @dt ) --秒数
set @dtInt = @dtInt*1000 + DATEPART(MILLISECOND,@dt) -- 毫秒数
RETURN @dtInt
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: GGA
-- Create date: 2013-03-28
-- Description: 根据毫秒数计算出时间
-- =============================================
CREATE FUNCTION [dbo].[ufn_GetTimeFormMSInt]
(
@msINT BIGINT
)
RETURNS DATETIME
AS
BEGIN
DECLARE @secInt BIGINT
DECLARE @leftMsInt BIGINT
SET @secInt=@msINT/1000;
SET @leftMsInt=@msINT%1000;
DECLARE @date DATETIME
SET @date = DATEADD(SECOND,@secInt,'1970-01-01 8:00:00:000')
SET @date = DATEADD(MILLISECOND,@leftMsInt,@date)
RETURN @date
END
GO