MS-SQL 字串String(含上午,下午) 轉 日期Datetime

使用情境

字串String(含上午,下午) 轉 日期Datetime

SQL語法

--日期有中文「上午」、「下午」轉換方法
create table #t
(
    DT varchar(50)
);
insert into #t
    (DT)
values
    ('2014/11/11 上午 09:15:00');
insert into #t
    (DT)
values
    ('2014/11/11 下午 09:15:00');
select DT as '修改前 (字串 String)', convert(varchar(19),convert(datetime, replace(replace(DT,'上午 ',''),'下午 ','')+case when charindex('上午',DT)>0 then 'AM' when charindex('下午',DT)>0 then 'PM' end),120) as '修改後 (日期 Datetime)'
from #t
drop table #t

輸出結果

訂閱
通知
guest
0 留言
預約回饋
查看所有留言