SQL Server将bigint列转换为日期时间

时间:2016-03-01 10:12:06

标签: sql-server datetime

我真的是msql的菜鸟,我需要帮助:

我有一个只有3列的表:

devicename, status, timeticks

timeticks是18位bigint值(例如 635924050579957219 ),我需要生成一个报告,其中timeticks显示为日期时间值。

我使用Microsoft SQL Server 2008 R2

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我在“将.NET Ticks转换为SQL Server DateTime”中尝试了提议的解决方案,我可以使用以下代码转换日期时间格式的刻度:

Declare @filetime_to_convert bigint
Set @filetime_to_convert = 635924050579957219

Declare @TickValue bigint
Declare @Days float

Set @TickValue = 635924050579957219
Select @Days = @TickValue * POWER(10.00000000000,-7) / 60 / 60 / 24

Select DATEADD(d, Cast(@Days As int), Cast('0001-01-01' As DATE)) 
    + Cast( (@Days - FLOOR(@Days)) As DateTime)

现在我需要将此插入到查询中以转换所有列'timeticks'(因为我告诉我真的是菜鸟)。

答案 1 :(得分:0)

create function dbo.converTicks(@filetime_to_convert bigint) returns datetime as begin

  Declare @TickValue bigint = @filetime_to_convert;
  Declare @Days float

  Select @Days = @TickValue * POWER(10.00000000000,-7) / 60 / 60 / 24

  return DATEADD(d, Cast(@Days As int), Cast('0001-01-01' As DATE)) 
      + Cast( (@Days - FLOOR(@Days)) As DateTime);

end;

go

select dbo.converTicks(635924050579957219) -- return 2016-03-01 04:57:37.927
-- you can use it like this:
select dbo.converTicks(t.ticksColumn) from yourTable t