Solved

Change variables in SQL table

Posted on 2016-09-14
6
104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 66

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 29

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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

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

Thanks you everyone for your help!
0
 
LVL 66

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

726 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