Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-12-27 : 09:04:42
|
i am looking for scrpit that replacr all characters unwanted like :',;[]-)( etcfor example:fantazy'cou ->fantazy_cougertoyt.frt-> gertoyt_frti want to run it on all the tables in my DB. |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-27 : 10:20:24
|
It depends on how many entries and size of the tables but two options areupdate tblset col = replace(replace(col,'.','_'),'-','_')where col like '%[.-]%'select 1while @@rowcount > 0update tblset col = stuff(col,patindex('%[.-]%',col),1,'_')where col like '%[.-]%'add replace statements and entries to the [.-] as needed - might have to escape some values.You cn generate the statments to run on all character columns in the database.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-12-28 : 01:35:43
|
how can i run it on all tables -all columns in my database ? |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-28 : 05:39:37
|
information_schema.columns will give you the columns in tables that are character datatype.I would put that list into a table so you can keep track of the progress (also include the number of rows and start and end time).Then you have to decide how you want to run it.If your database is fairly small then I would go for the second process I suggested. You might want to start using that on small tables anyway.You might create an sp to run itcreate proc s_conv@tbl varchar(128) ,@col varchar(128)asdeclare @sql varchar(4000)select @sql = 'select 1while @@rowcount > 0update [' + @tbl + ']set [' + @col + '] = stuff(col,patindex(''%[.-]%'',[' + @col + ']),1,''_'')where [' + @col + '] like '%[.-]%''gothen call it viadeclare @tbl varchar(128), @col varchar(128)while exists (select * from convtbl where endtime is nullbeginselect top 1 @tbl = tbl, @col = colfrom convtblwhere endtime is nullorder by rowcountupdate convtblset starttime = getdate()where @tbl = tbl and @col = colexec s_conv @tbl, @colupdate convtblset endtime = getdate()where @tbl = tbl and @col = colend==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|