473,698 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do i replace a substring in sqlserver ?

Hi
I have a character field, varchar(60), and I want to replace only a part
of it, a set substring which is always in the same set positions.
Does anyone have an Sql statement or algorithm/sp that could do that
with one call or run ?
Thanks
David

Feb 6 '06 #1
3 34035
here is an example of SUBSTRING/REPLACE

CREATE TABLE #temp(stringRep VARCHAR(60))
INSERT INTO #temp(stringRep ) VALUES('hazyCow ')
INSERT INTO #temp(stringRep ) VALUES('lazyCow ')

DECLARE @startPos INT
SET @startPos = 2
UPDATE #temp SET stringRep = REPLACE(stringR ep,SUBSTRING(st ringRep, 2,
3),'zz')

SELECT * FROM #temp

DROP TABLE #temp

--
Jack Vamvas
_______________ _______________ _______________ _______________ ______
Receive free SQL tips - register at www.ciquery.com/sqlserver.htm
SQL Server Performance Audit - check www.ciquery.com/sqlserver_audit.htm
New article by Jack Vamvas - SQL and Markov Chains -
www.ciquery.com/articles/art_04.asp

"David Greenberg" <da*****@iba.or g.il> wrote in message
news:43******** ******@iba.org. il...
Hi
I have a character field, varchar(60), and I want to replace only a part
of it, a set substring which is always in the same set positions.
Does anyone have an Sql statement or algorithm/sp that could do that
with one call or run ?
Thanks
David

Feb 6 '06 #2
David Greenberg (da*****@iba.or g.il) writes:
I have a character field, varchar(60), and I want to replace only a part
of it, a set substring which is always in the same set positions.
Does anyone have an Sql statement or algorithm/sp that could do that
with one call or run ?


UPDATE tbl
SET col = substring(col, 1, 20) + 'newval' + substring(col, 26, 60)
FROM tbl
WHERE ...
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 6 '06 #3
On Mon, 06 Feb 2006 17:50:01 +0200, David Greenberg wrote:
Hi
I have a character field, varchar(60), and I want to replace only a part
of it, a set substring which is always in the same set positions.
Does anyone have an Sql statement or algorithm/sp that could do that
with one call or run ?
Thanks
David


Hi David,

Look up the STUFF function in Books Online - this will do exactly what
you want.

--
Hugo Kornelis, SQL Server MVP
Feb 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4764
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots of</br> inside it... Being <br/> HTML tags and not simple text like &lt;br/&gt;
5
39647
by: mp | last post by:
when I write this in c# : strFileToLoad = strFileToLoad.Substring(0, 2) + strFileToLoad.Substring(2).Replace("\\", "\"); it doesnt like the last "\", and I want to replace \\ with \ what should I do? Thanks
4
3850
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
1
6710
by: MCH | last post by:
Hi there, I am using std:string and try to replace a substring with replace function. From the document, I found one prototype of string.replace basic_string& replace(iterator first, iterator last, const basic_string& s) I am going to replace the substring specified by first and last and replace it with s, where s may longer or short than the substring being replaced. If the replace function suitable for my case ? Anyway, I use
1
2898
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of fewer than 80 characters. It then prompts the user to type the search-substring of 10 or fewer characters. Finally, it prompts the user to type the replace-substring of 10 or fewer characters. The second function call is the search-and-replace...
6
15581
by: JackpipE | last post by:
Here is my replace query and I need to run this on every column in my table. Right now I manually enter the column name (_LANGUAGES_SPOKEN) but this is time consuming and would like to automate this process as much as possible. Update PROFILE SET LANGUAGES_SPOKEN = replace(cast(_LANGUAGES_SPOKEN as nvarchar(255)),char(13)+char(10),':') Thanks,
0
925
by: ambelkar | last post by:
hi, I have written the following code to get MonthEndDate,RunDate,MonthStartDate. It is giving year in MonthStartDate 2005, upto sep07 it is ok.When it come to OCT 07 it has to display monstartdate year as 2006.How can I do this.Please help me SELECT REPLACE(upper(SUBSTRING(CONVERT(varchar, GETDATE() - DAY(GETDATE()), 113), 1, 11)), ' ', '-') as MonthEndDate, '01' + '-' + UPPER(substring(datename(m,getdate()),1,3)) +...
25
2286
by: magix | last post by:
Hi, I have char* str1 = "d:\temp\data\test.txt" I want to replace all the "\" to be "\\", so that the string will have "d:\\temp\\data\\test.txt" Can you help ?
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.