舍入毫秒SQL Server 2000

时间:2015-01-19 14:23:44

标签: sql sql-server sql-server-2000

在Microsoft SQL Server 2000中,我有这些数据。

1900-01-01 00:10:10.830
1900-01-01 00:10:10.430

从上面的列中,我想选择日期时间并舍去毫秒,以获得以下输出

1900-01-01 00:10:11
1900-01-01 00:10:10

提前致谢

2 个答案:

答案 0 :(得分:1)

对于SQL Server 2008及更高版本,您可以使用DATETIME2。 SQL Server 2008及更高版本中提供了DATETIME2 - 有关详细信息,请参阅here

SELECT CAST('1900-01-01 00:10:10.830' AS DATETIME2(0));
SELECT CAST('1900-01-01 00:10:10.430' AS DATETIME2(0));

Confirmed Output

对于早期版本的SQL Sever,例如SQL Server 2000.您可以执行以下操作:

SELECT DATEADD(ms, -DATEPART(ms, DATEADD(ms, 500, CAST('1900-01-01 00:10:10.830' AS DATETIME))) , DATEADD(ms, 500, CAST('1900-01-01 00:10:10.830' AS DATETIME)));
SELECT DATEADD(ms, -DATEPART(ms, DATEADD(ms, 500, CAST('1900-01-01 00:10:10.430' AS DATETIME))) , DATEADD(ms, 500, CAST('1900-01-01 00:10:10.430' AS DATETIME)));

答案 1 :(得分:0)

SELECT *
     , DateAdd(ss, rounded_second, round_down_seconds) As result
FROM   (
        SELECT *
             , Round(nanoseconds / 1000.0, 0) As rounded_second
        FROM   (
                SELECT the_date
                     , DatePart(ms, the_date) As nanoseconds
                     , DateAdd(ss, DateDiff(ss, 0, the_date), 0) As round_down_seconds
                FROM   (
                        SELECT '1900-01-01 00:10:10.830' As the_date
                        UNION ALL 
                        SELECT '1900-01-01 00:10:10.430'
                       ) As x
               ) As y
       ) As z

我已将每一步分开,尽可能清楚。

如果你想要一个衬垫:

SELECT the_date
     , DateAdd(ss, Round(DatePart(ms, the_date) / 1000.0, 0), DateAdd(ss, DateDiff(ss, 0, the_date), 0)) As result
FROM   (
        SELECT '1900-01-01 00:10:10.830' As the_date
        UNION ALL 
        SELECT '1900-01-01 00:10:10.430'
       ) As x