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
Solved

Stored procedure to trim and replace

Posted on 2007-03-29
7
224 Views
Last Modified: 2010-04-23
I need a very simple stored procedure to be executed manually from Query Analyzer which will go through every record in a table and replace the first character of the value of a given field with a new one:

for example, I want this sp to go through every record in TABLE1 and replace the "E" from "E:\*--some path--*" with "F:\*--some path--" in a field called PATH1.

I need this solution ASAP and willing to award the maximum points.
0
Comment
Question by:shirvanians
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 18820118
try this:

UPDATE TABLE1
SET PATH1='F'+SUBSTRING(PATH1,2,LEN(PATH1)-1)
WHERE ISNULL(PATH1,'')<>''
0
 
LVL 11

Expert Comment

by:dready
ID: 18820158
The above solution might be a bit dangerous, it will just replace every first character in the path1 column with an F.

Better is this:
Update table1 set path1 = REPLACE(path1,'E:\','F:\')
0
 
LVL 13

Accepted Solution

by:
Wizilling earned 300 total points
ID: 18820191
DECLARE @sql NVARCHAR(1000)
SET @sql = 'UPDATE ?
        SET PATH1=''F''+SUBSTRING(PATH1,2,LEN(PATH1)-1)
      WHERE PATH1 like ''E%''
'

EXEC sp_MSForeachtable @sql
0
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.

 
LVL 13

Expert Comment

by:Wizilling
ID: 18820208
ignore my statemebnt. i thought u wanted to go through every table.
misunderstood the question. my bad.
0
 

Author Comment

by:shirvanians
ID: 18820318
Hi Wizilling,
I think your suggestion will work. Am I supposed to replace the ? after UPDATE with the table name?

LIKE THIS:

DECLARE @sql NVARCHAR(1000)
SET @sql = 'UPDATE TABLE1
        SET PATH1=''F''+SUBSTRING(PATH1,2,LEN(PATH1)-1)
      WHERE PATH1 like ''E%''
'

EXEC sp_MSForeachtable @sql
0
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 200 total points
ID: 18820336
UPDATE TABLE1
SET PATH1='F'+SUBSTRING(PATH1,2,LEN(PATH1)-1)
WHERE LEFT(PATH1,1)='E'
0
 

Author Comment

by:shirvanians
ID: 18820380
Fantastic, it worked. Let's split the points between the two of you geniuses...
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

789 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