Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace one char in a string

Posted on 2004-08-31
3
Medium Priority
?
227 Views
Last Modified: 2006-11-17
I need to replace the : in 'My Files: username' with a - to make it 'My Files- Username'.

The column name is [name] and I have to do this for all the rows in the table where [name] like 'My Files:%'.

How can I do this? (I know I will use a cursor to extract all rows and some form of substring and charindex.)

I need a quick answer to be honest.
Thanks.

0
Comment
Question by:danielvic
  • 2
3 Comments
 
LVL 3

Expert Comment

by:Minna
ID: 11948987
Update <table>
set [name] = replace([name], ':', '-')
0
 
LVL 3

Accepted Solution

by:
Minna earned 2000 total points
ID: 11949018
Oh I forgot to mention, that query will update all your rows without the need for cursors.
And add your where clause for better performance

Update <table>
set [name] = replace([name], ':', '-')
where [name] like 'My Files:%'
0
 

Author Comment

by:danielvic
ID: 11949060
well, that was easy! didn't know about the replace function.
I did a search for replace substring before posting this question and didn't find any hits with replace function in the answer (?!).
Thanks for the quick reply. It works (of course).
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

879 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