Convert UTC Milliseconds to DATETIME in SQL server

前端 未结 6 1021
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 18:56

I want to convert UTC milliseconds to DateTime in SQL server.

This can easily be done in C# by following code:

DateTime startDate = new DateTime(1970         


        
6条回答
  •  忘掉有多难
    2020-12-02 19:38

    The DATEADD requires an integer as a second argument. Your number 1348203320000 is very large for integer therefore it produce an error in runtime. Your should use bigint type instead and provide DATEADD with correct int values by splitting your milliseconds to seconds and milliseconds. That is sample you could use.

    DECLARE @total bigint = 1348203320000;
    
    DECLARE @seconds int = @total / 1000
    DECLARE @milliseconds int = @total % 1000;
    
    DECLARE @result datetime = '1970-1-1';
    SET @result = DATEADD(SECOND, @seconds,@result);
    SET @result = DATEADD(MILLISECOND, @milliseconds,@result);
    SELECT @result
    

提交回复
热议问题