当前位置: 代码迷 >> Sql Server >> 字段串处理有关问题
  详细解决方案

字段串处理有关问题

热度:81   发布时间:2016-04-27 16:50:38.0
字段串处理问题
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)
  相关解决方案