无法按日期时间排序设置行号

Not able to set row number by ordered by datetime

我目前尝试为以下 table 中的行提供索引

    ticket_id(bigint)       val(nvarchar(4000)) 
    99856                   15.01.2019 16:58    
    99856                   15.01.2019 17:20    
    99921                   15.01.2019 17:31    
    100197                  16.01.2019 09:55    

索引应该由以下逻辑生成

    row_number() over (Partition by ticket_id Order by convert(datetime,val,104) asc) as nr

但是,这个查询 returns

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

当我使用以下查询时有效

    row_number() over (Partition by cast(ticket_id as int) Order by convert(datetime,val,104) asc) as nr

有人可以解释为什么吗?

转换不正确,尝试查找错误值

SELECT * FROM [youTable] WHERE TRY_CONVERT(datetime,val,104) IS NULL

更新

when I apply "Try_convert", I do not get any Error

TRY_CONVERT

Return Types
Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.

例如

SELECT TRY_CONVERT(datetime,'99.99.9999',104)

我希望你的日期格式是 dd.mm.yyyy 。所以你可以在下面查询可能对你有帮助

select convert (datetime ,'15.01.2019 16:58 ',103)

你的情况

row_number() over (Partition by ticket_id Order by convert(datetime,val,103) asc) as nr