Declare @YourTable table (TimeStr varchar(25)) Insert Into @YourTable values ('151200'), ('085800'), ('080100'), ('210100'), ('083300') Select * ,AsTime = cast(stuff(stuff(TimeStr,5,0,':'),3,0,':') as time) From @YourTable
退货
TimeStr AsTime 151200 15:12:00.0000000 085800 08:58:00.0000000 080100 08:01:00.0000000 210100 21:01:00.0000000 083300 08:33:00.0000000
编辑:
根据您的版本,考虑将时间值存储在varchar中(这意味着您可能会有假数据),可以使用:
try_convert(time,stuff(stuff(TimeStr,5,0,':'),3,0,':'))