Solved

SQL Query replace part of string

Posted on 2011-03-09
3
885 Views
Last Modified: 2012-05-11
I am looking for an example of an SQL Query for MS SQL Server which woudl allow me to replace a part of the string inside the record's field.
For example:
Select * where ItemName LIKE 'O:\location1\%'
The full location example is O:\Location1\filename42a62bcf.dbs
Since the location of the files have been changed I need to preserve the rest of the record but update only "ItemName" field 'O:\location1' with "D:\Location2"
Does anyone have an example please?
0
Comment
Question by:sstouk
3 Comments
 
LVL 2

Accepted Solution

by:
DowntownIT earned 500 total points
ID: 35087089
Here is a basic version. Let me know if you need more


SELECT 'O:\Location1\filename42a62bcf.dbs' AS ItemFile INTO #tmp

SELECT * FROM #tmp

UPDATE #tmp
SET ItemFile = REPLACE(ItemFile,'O:\Location1\','D:\Location2\')
SELECT * FROM #tmp
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35087105

update table1
set ItemName := REPLACE(ItemName, 'O:\location1', D:\Location2)
where ItemName LIKE 'O:\location1\%'
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35087214
update table1
set ItemName = REPLACE(ItemName, 'O:\location1', 'D:\Location2')
where ItemName LIKE 'O:\location1\%'

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OPENDATASOURCE 8 35
How to automate & schedule this Index optimization Script ? 10 76
Another way of doing this SQL 8 46
Problem with SqlConnection 4 157
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 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

9 Experts available now in Live!

Get 1:1 Help Now