sql server - Need to insert breaks in strings of very column with ' ' or ',' -
i have table has 1 column on 100,000 rows
col_name qwchijhuirhxnihdiuyfnx
dhjhfiurhncnmxmzjcoinrds xnbxknsiiuncirnxknrxnxz
i need insert '.' or '$' or marker after every 3rd character
example of result needed:
col_name qwc.hij.hui.rhx.nih.diu.yfn.x dhj.hfi.urh.ncn.mxm.zjc.oin.rds. xnb.xkn.sii.unc.irn.xkn.rxn.xz
i solved with:
insert new_table ( c1 ,c2 ,c3 ) select substring(cast(col_name varchar(max)),1,3) c1 ,substring(cast(col_name varchar(max)),4,3) c2 ,substring(cast(col_name varchar(max)),7,3) c3 table_name
this causes problems later in script data must remain in 1 column inserted new table long new table 1 column
here's sqlfiddle starting point can refactor http://sqlfiddle.com/#!6/ab6dd/1/0 using function , while loop.
you may able more efficient regular expressions or sqlclr if need speed.
create function dotify (@input varchar(max)) returns varchar(max) begin declare @output varchar(max) = '' declare @index int = 0 declare @length int set @length = len(@input) while @index <= @length begin set @output = @output + substring(@input, @index, 1) if (@index % 3) = 0 , @index > 0 begin set @output = @output +'.' end set @index = @index + 1 end return(@output) end go select top 10000 col_name, dbo.dotify(col_name) old_table
you can use top limit processing time few seconds can profile efficiency changes make.
Comments
Post a Comment