Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I really enjoy your site. You have a lot of helpful and friendly experts who contribute so willingly. Thank you for past (and future) technical advice..."

Geography

Where in the world do Tek-Tips members come from?
markdmac (MIS)
14 Jun 12 16:33
I've been asked to step in and take over a migration project. The company that setup the migration tools transposed letters in the domain name. Can anyone assist me with SQL update code to search all databases for the text LSV\ and replace it with LVS\?

Regretfully I don't know what field this will be in and the database has several hundred tables to search, so I am hoping to be able to just plug in the database name which is QMM and have code walk the database.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.

Helpful Member!  gmmastros (Programmer)
14 Jun 12 18:19
Mark,

If you run the query below, it will generate all of the replace statements you will need, but not actually run the replace statements. When I run the code below on my database, I get 1840 rows in the output. You could then copy/paste the output to a new query window, verify the statements (removing any you don't want to run) and then execute them. I hope this is enough to be helpful.

CODE

Select 'Update [' + table_schema + '].[' + table_name + '] ' +
'Set [' + column_name + '] = Replace([' + column_name + '], ''LSV\'', ''LVS\'') ' +
'Where [' + column_name + '] Like ''%LSV\%'''
From Information_Schema.Columns
where data_type Like '%char%'
and character_maximum_length > 3

Note this this code limits the columns to just those that are varchar, nvarchar, char and nchar and only operates on columns where the defined max size is greater than 3 characters.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

dhulbert (TechnicalUser)
15 Jun 12 3:20

Mark.

George's script above will give you any columns with the appropraiate text in the column name.

Are you looking for that or do you need to find the text in individual records in a table?

I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)

gmmastros (Programmer)
15 Jun 12 9:42
dhulbert,

You are mistaken. The query I show above does not look for the appropriate text in the column name. Instead, it generates a sql query for each column that has a string type with maxlength exceeding 3 characters.

When I run the code on my database, the output looks like this"


Update [dbo].[Address]
Set [StreetName] = Replace([StreetName], 'LSV\', 'LVS\')
Where [StreetName] Like '%LSV\%'

Update [dbo].[Address]
Set [StreetType] = Replace([StreetType], 'LSV\', 'LVS\')
Where [StreetType] Like '%LSV\%'

Update [dbo].[Address]
Set [Suffix] = Replace([Suffix], 'LSV\', 'LVS\')
Where [Suffix] Like '%LSV\%'

Update [dbo].[Address]
Set [Address2] = Replace([Address2], 'LSV\', 'LVS\')
Where [Address2] Like '%LSV\%'

Update [dbo].[Address]
Set [City] = Replace([City], 'LSV\', 'LVS\')
Where [City] Like '%LSV\%'


I added the line breaks manually so that the output would be clearer to understand.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

dhulbert (TechnicalUser)
15 Jun 12 10:04

Apologies George.

Quick skim first thing in the morning before coffee.

I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)

markdmac (MIS)
21 Jun 12 21:44
No worries guys. I ended up having to check each table manually but got all instances fixed. The code that was generated did not seem to identify tables that actually had the offending value.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close