【问题标题】:SQL Server - How to remove trailing zeros from milliseconds in datetime2 data typeSQL Server - 如何从 datetime2 数据类型的毫秒中删除尾随零
【发布时间】:2018-07-18 03:10:06
【问题描述】:

如何使用任何强制转换或转换函数从 datetime2 数据类型的毫秒中删除尾随零。

例如,我有这些数据:

2018-02-17 13:26:55.033000
2018-02-17 12:37:12.300000
2018-02-17 14:55:30.110000

我想改成:

2018-02-17 13:26:55.033
2018-02-17 12:37:12.3
2018-02-17 14:55:30.11

提前致谢。

【问题讨论】:

  • 使用datetime 而不是datetime2。或使用CAST()CONVERT() 转换为日期时间
  • @Squirrel 我用过这个 convert(datetime, convert(datetime2, '2018-02-17 12:37:12.300000')) 但它不起作用。结果是 2018-02-17 12:37:12.300。
  • 您还想删除任何尾随零吗?格式化应该在显示数据的前端应用程序上完成,而不是在 SQL Server 中。删除任何尾随零意味着,将 varchar 返回到您的前端应用程序,而不是 datetime 了

标签: sql sql-server datetime2


【解决方案1】:
-- Sample DDL/DML statements:
declare @tbl table (dt datetime2)
insert into @tbl values 
('2018-02-17 13:26:55.033000'),
('2018-02-17 12:37:12.300000'),
('2018-02-17 14:55:30.110000')

-- Use this recursive CTE to trim trailing zeros.
-- The dt col is the original value, the rn col is a 
-- recursive incrementor, and the vc col is the varchar
-- representation of the datetime2.
;with cte as 
(
    select dt, 1 as rn, cast(dt as varchar(100)) vc 
    from @tbl
    union all 
    select dt, rn + 1, SUBSTRING(vc, 1, len(vc) - 1)
    from cte 
    where SUBSTRING(reverse(vc), 1, 1) = '0'
)
select vc 
from cte 
where rn = (select max(rn) from cte c where dt = cte.dt)

vc
2018-02-17 14:55:30.11
2018-02-17 12:37:12.3
2018-02-17 13:26:55.033

【讨论】:

    【解决方案2】:

    试试这个。 AS Squirrel 提到这将返回 nvarchar 而不是 datetime。

    CREATE TABLE #temp
        (
            YourColumn DATETIME2
        );
    
    INSERT INTO #temp ( YourColumn )
    VALUES ( GETDATE() -- YourColumn - datetime2
        );
    
    SELECT *
    FROM   #temp;
    
    SELECT REVERSE(
               SUBSTRING(
                   REVERSE(CONVERT(VARCHAR(100), YourColumn, 121)) ,
                   PATINDEX(
                       '%[1-9]%' , REVERSE(CONVERT(VARCHAR(100), YourColumn, 121))),
                   LEN(YourColumn)
                   - PATINDEX(
                         '%[1-9]%' ,
                         REVERSE(CONVERT(VARCHAR(100), YourColumn, 121))) + 1)) AS YourColumnTrim
    FROM   #temp;
    
    DROP TABLE #temp;
    

    结果:

    +-----------------------------+
    |         YourColumn          |
    +-----------------------------+
    | 2018-07-18 13:22:54.1530000 |
    +-----------------------------+
    
    +-------------------------+
    |     YourColumnTrim      |
    +-------------------------+
    | 2018-07-18 13:22:54.153 |
    +-------------------------+
    

    【讨论】:

    • 数据类型是 datetime2 而不是 strig
    • 抱歉,忘记将其转换为字符串。
    • 可以在不将 Datetime2 转换为 varchar 的情况下工作。
    • 那是因为datetime2varchar 的隐式转换使用的是121 样式。转换数据类型时最好明确指定样式
    • 感谢您的解决方案。谢谢大家。
    【解决方案3】:

    获取毫秒部分,将其反转,将其转换为 INT 并再次反转。请尝试-。

    ;WITH CTE AS
    (
        SELECT 
    
        GETDATE() CompleteDate,
    
        CAST ( GETDATE() AS DATE ) DtPart   
    
        ,CONVERT(VARCHAR, GETDATE () , 24 )  TmPart
    
        ,REVERSE(CAST(REVERSE(DATEPART(MILLISECOND,GETDATE())) AS INT)) MilliPart   
    )
    SELECT CompleteDate 
        , CONCAT( DtPart, ' ' , TmPart , '.' , MilliPart ) [DateTimeWithOutZero]
    FROM CTE
    
    
    CompleteDate            DateTimeWithOutZero
    ----------------------- ------------------------------------------------------------------------------------
    2018-07-18 14:22:52.190 2018-07-18 14:22:52.19
    
    (1 row affected)
    

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多