Solved

Change variables in SQL table

Posted on 2016-09-14
6
93 Views
Last Modified: 2016-09-15
Hi,

How do I change all highlighted to D: drive in SQL with one query.

view.jpg
0
Comment
Question by:Member_2_7967487
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41798913
Use the T-SQL Replace keyword to change subsets of string values.  Will probably want to do this a couple of times to handle all the upper case / lower case scenarios.

UPDATE dbo.Company
SET 
   ACCTdirect = REPLACE (ACCTdirect, 'C:\DataMasons' , 'D:\'), 
   EDIdirect = REPLACE (EDIdirect , 'C:\DataMasons' , 'D:\'), 
   vpEDIdirect = REPLACE (vpEDIdirect, 'C:\DataMasons' , 'D:\'), 
   vpSharedirect = REPLACE (ACCTdirect, 'C:\DataMasons' , 'D:\')

Open in new window

0
 
LVL 42

Expert Comment

by:pcelba
ID: 41798941
You don't need to execute the command several times:
UPDATE dbo.Company
SET 
   ACCTdirect = REPLACE (ACCTdirect, 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:'), 
   EDIdirect = REPLACE (EDIdirect , 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:'), 
   vpEDIdirect = REPLACE (vpEDIdirect, 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:'), 
   vpSharedirect = REPLACE (ACCTdirect, 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:')

Open in new window

Of course, if your columns are case insensitive already then the COLLATE is not necessary.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41799068
Try below

UPDATE dbo.Company
SET
   ACCTdirect = REPLACE (ACCTdirect, 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:'),
   EDIdirect = REPLACE (EDIdirect , 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:'),
   vpEDIdirect = REPLACE (vpEDIdirect, 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:'),
   vpSharedirect = REPLACE (ACCTdirect, 'C:\DataMasons' COLLATE Latin1_General_CI_AS , 'D:')
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:Member_2_7967487
ID: 41799899
Jim's feedback works best. Thank you, Jim!!

Thanks you everyone for your help!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41799940
Thanks for the accept.  Good luck with your project.  -Jim

@pcelba and pawan - I'm glad you guys posted, as I didn't know you can combine REPLACE with COLLATE.  I can predict sitting for a SQL 2016 T-SQL exam and getting that as a question, and not answering it correctly if it were not for reading your posts.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 41800070
BTW, I wouldn't say Jim's solution works best it was just faster but it also produces double backslashes on output...  :-)

np for me
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question