sql server 2008 - SQL splitting a word in separate characters -
i need change application , first thing need change field in database table. in table have 1 6 single characters, i.e. 'abcdef' need change '[a][b][c][d][e][f]'
[edit] meant stay in same field. before field = 'abcdef' , after field = '[a][b][c][d][e][f]'.
what way this?
rg. eric
you can split string separate characters using following function:
create function ftstringcharacters ( @str varchar(100) ) returns table return v1(n) ( select 1 union select 1 union select 1 union select 1 union select 1 union select 1 union select 1 union select 1 union select 1 union select 1 ), v2(n) (select 1 v1 a, v1 b), v3(n) (select top (isnull(datalength(@str), 0)) row_number() on (order @@spid) v2) select n, substring(@str, n, 1) c v3 go
and apply as:
update t set t.fieldname = p.fieldmodified tablename t cross apply ( select (select quotename(s.c) ftstringcharacters(t.fieldname) s order s.n xml path(''), type).value('text()[1]', 'varchar(20)') ) p(fieldmodified)
Comments
Post a Comment