Solved

Stored procedure to trim and replace

Posted on 2007-03-29
7
227 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
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…

622 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