Solved

Stored procedure to trim and replace

Posted on 2007-03-29
7
222 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Save conflict messages on existing documents 15 43
Advice on Xojo as a development tool over VB. 4 31
grouping logic 6 46
Variable Event ? 3 19
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now