日期:2014-05-18  浏览次数:20441 次

字段串处理问题
A表如下:
--------------------------------------
Event                                                                                                                                     PC
Connection   to   user   25952   (PYGP01   ),   terminal   15   (PYC239   )   lost

要求如下:
将第二个小括号里的值,更新到PC字段


------解决方案--------------------
create table A(Event varchar(100), PC varchar(100))
insert A select 'Connection to user 25952 (PYGP01 ), terminal 15 (PYC239 ) lost ', null

update A set PC=substring(Event,
charindex( '( ', Event, charindex( '( ', Event)+1)+1,
charindex( ') ', Event, charindex( ') ', Event)+1)-charindex( '( ', Event, charindex( '( ', Event)+1)-1
)

select * from A

--result
Event PC
---------------------------------------------------------------- ----------------------------------------------------------------
Connection to user 25952 (PYGP01 ), terminal 15 (PYC239 ) lost PYC239

(1 row(s) affected)