MS SQL中bigint 与 datetime之间的转换

1. sql语句中得到JAVA长整型的当前时间

declare @aIn_date bigint

set @aIn_date=DATEDIFF ( second , '1970-01-01 08:00:00.000' , getdate() ) -- 秒数
set @aIn_date = @aIn_date*1000 + datepart(ms,getdate()) -- 毫秒数


2. 以datetime类型方式,显示表中bigint型的时间

select DATEADD(HOUR, DATEDIFF(HOUR,'19000101 00:00', '19700101 08:00'), CAST(create_date / (1000.0*3600*24.0) AS DATETIME)) from t_test

说明, create_date是个bigint类型的字段

写个自定义函数来显示更方便:

-- =============================================
-- Author: Michael.Du
-- Create date: 2010-06-23
-- Description: 将bigint日期转为datetime型,方便查看 (本函数目前只是用于调试查看,没有参与实际生产)
-- 调用示例:
-- select dbo.toDateTime(create_date) from XXXX
-- =============================================
ALTER FUNCTION [dbo].[toDateTime](
@bigint_date bigint
)
RETURNS datetime
AS
BEGIN
declare @aa datetime

select @aa = DATEADD(HOUR, DATEDIFF(HOUR,'19000101 00:00', '19700101 08:00'), CAST(@bigint_date / (1000.0*3600*24.0) AS DATETIME))

RETURN @aa

END

你可能感兴趣的:(DateTime)