我的表格中包含VARCHAR
列,其中包含时间。价值如下所示:
TXN_TIME
--------
053124
173932
011815
120349
134207
我需要转换它
TXN_TIME
--------
05:31:24:000000
17:39:32:000000
01:18:15:000000
12:03:49:000000
13:42:07:000000
请举一些例子..
答案 0 :(得分:1)
不清楚您的结果是VARCHAR
还是实际的TIME
类型,所以这两者都是:
DECLARE @Table TABLE (
TXN_TIME VARCHAR(6)
)
INSERT INTO @Table
SELECT '053124'
UNION
SELECT '173932'
UNION
SELECT '011815'
UNION
SELECT '120349'
UNION
SELECT '134207'
SELECT TXN_TIME,
LEFT(TXN_TIME,2) + ':' + SUBSTRING(TXN_TIME,3,2) + ':' + SUBSTRING(TXN_TIME,5,2) + ':000000' AS Converted,
CONVERT(TIME(6),LEFT(TXN_TIME,2) + ':' + SUBSTRING(TXN_TIME,3,2) + ':' + SUBSTRING(TXN_TIME,5,2)) AS ConvertedTime
FROM @Table
结果:
/----------------------------------------------\
| TXN_TIME | Converted | ConvertedTime |
|----------+-----------------+-----------------|
| 011815 | 01:18:15:000000 | 01:18:15.000000 |
| 053124 | 05:31:24:000000 | 05:31:24.000000 |
| 120349 | 12:03:49:000000 | 12:03:49.000000 |
| 134207 | 13:42:07:000000 | 13:42:07.000000 |
| 173932 | 17:39:32:000000 | 17:39:32.000000 |
\----------------------------------------------/