我有这种类型的数据
alt text http://img46.imageshack.us/img46/6351/timed.png
TimeOFDay列是varchar。我想在24小时内更改此时间,使用SQL并更新TwentyFourHourTime列。 TwentyFourHourTime列也是varchar。
我该怎么办呢。
感谢。
答案 0 :(得分:0)
尝试以下查询:
update tableName set TwentyFourHourTime =
case TimeMeridiem
WHEN 'PM' THEN Convert(varchar,dateadd(hour,12,TimeOFDay),108)
ELSE Convert(varchar,TimeOFDay,108)
end
答案 1 :(得分:0)
我写过这个测试 - 似乎有效。适用于午夜并添加前导零。
<强>更新:强>
DROP TABLE #times
CREATE TABLE #times
(
TimeOfDay VARCHAR(32),
TimeMeridiem VARCHAR(32),
TwentyFourHourTime VARCHAR(32)
)
INSERT INTO #times (TimeOfDay, TimeMeridiem) VALUES ('1:00', 'PM')
INSERT INTO #times (TimeOfDay, TimeMeridiem) VALUES ('1:00', 'AM')
INSERT INTO #times (TimeOfDay, TimeMeridiem) VALUES ('12:00', 'PM')
UPDATE #times SET TwentyFourHourTime = REPLACE(Right(Replicate('0',5) +
REPLACE((SELECT CASE WHEN TimeMeridiem = 'PM' THEN
CAST((SELECT REPLACE(TimeOfDay,':','.')) AS dec(10,2)) + 12 ELSE
(SELECT REPLACE(TimeOfDay,':','.')) END), '24.', '00.') ,5), '.', ':')
SELECT * FROM #times
结果:
TimeOfDay TimeMeridiem TwentyFourHourTime
--------- ------------ ------------------
1:00 PM 13:00
1:00 AM 01:00
12:00 PM 00:00