declare @dt datetime2(3)='2015-05-07 10:05:23.187'
declare @dt_bi varbinary(max)=convert(varbinary(max), @dt)
select @dt_bi as date_time_binary
,convert(varbinary(max),reverse(@dt_bi)) as reverse_binary
把二进制值拆分成DateTime2(3)的各个组成成分:
declare @dt datetime2(3)='2015-05-07 10:05:23.187'
declare @dt_bi varbinary(max)=convert(varbinary(max), @dt)
declare @dt_bi_littleEnd varbinary(max)
select @dt_bi_littleEnd=convert(varbinary(max),reverse(@dt_bi))
select substring(convert(varbinary(8), @dt_bi_littleEnd), 1, 3) as date_binary,
cast(substring(convert(varbinary(8), @dt_bi_littleEnd), 1, 3) as int) as date_int,
substring(convert(varbinary(8), @dt_bi_littleEnd), 4, 4) as time_binary,
cast(substring(convert(varbinary(8), @dt_bi_littleEnd), 4, 4) as int) as time_int,
substring(convert(varbinary(8), @dt_bi_littleEnd), 8, 1) as precision_binary,
cast(substring(convert(varbinary(8), @dt_bi_littleEnd), 8, 1) as int) as precision_int;
3,利用偏移量和基准还原原始值
有了偏移量,就可以在基准日期和时间之上加上偏移量来获得原始值:
declare @Time time='00:00:00.000'
declare @Date date='0001-01-01'
select dateadd(day, 735724, @Date) as originl_date
, dateadd(ms,36323187, @Time) as original_time
参考文档:
What is the SQL Server 2008 DateTime2 Internal Structure?
How to Get SQL Server Dates and Times Horribly Wrong 总结
以上所述是小编给大家介绍的SQL Server 日期和时间的内部存储,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!