473,804 Members | 4,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MSSQL Replace() for first occurance?

2 New Member
Hi!

Is there any way to get the Replace() function in MSSQL to replace only the first occurence of a substring? For example, my Name column has the following data:

Bob
BobBob

And I want to replace "Bob" with "Robert," but *only* the first occurence:

Robert
RobertBob

Unfortunately, the length of the original string, the searchFor substring, and the replaceWith substring all vary. Any idea what the most efficient way to do this might be?
Nov 3 '06 #1
1 11923
hairlessOrphan
2 New Member
Well, I gave it a shot. I don't know if there's a better solution than this; if you can think of one, please post!

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION ReplaceFirst (@origStr varchar(8000), @searchFor varchar(8000), @replaceWith varchar(8000)) 
  2. RETURNS varchar(8000) AS  
  3. BEGIN 
  4.     DECLARE @start AS int
  5.     DECLARE @end AS int
  6.     DECLARE @newStr AS varchar(8000)
  7.  
  8.     SET @start = CHARINDEX(@searchFor, @origStr)
  9.  
  10.     IF (@start = 0)
  11.         BEGIN
  12.             SET @newStr = @origStr
  13.         END
  14.     ELSE
  15.         BEGIN
  16.             SET @end = @start + LEN(@searchFor)
  17.             SET @newStr = SUBSTRING(@origStr, 1, @start - 1) + @replaceWith + SUBSTRING(@origStr, @end, LEN(@origStr) + 1 - @end)
  18.         END
  19.     RETURN @newStr
  20. END
  21.  
Nov 3 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
8625
by: Little PussyCat | last post by:
Hello, I need to be able to replace only the first occurance of a space character in a column. Reason being is the data in the column I am trying to replace seems to have umpteen space characters after each entry, so a simple replace function will replace all the spaces after it with what I want! I have thought of RTRIM to get rid of the spaces after and then replace, I have also thought of CHARINDEX to find the first occurance of a...
8
14024
by: Yusuf INCEKARA | last post by:
I have a stored procedure : CREATE PROCEDURE STP_GETSTORELIST @RETCUR CURSOR VARYING OUTPUT AS set @RETCUR = CURSOR FORWARD_ONLY STATIC FOR SELECT ID,STORE_NAME FROM T_INF_STORE ORDER BY STORE_NAME OPEN @RETCUR
1
1429
by: RSH | last post by:
Hi, i have a situation where I have a string read from a file. The string format looks like this <HEADERROWdynamic number of characters </HEADERROW> dynamic number of rows <HEADERROWdynamic number of characters </HEADERROW> dynamic number of rows <HEADERROWdynamic number of characters </HEADERROW>
8
17515
by: Joe Cool | last post by:
I need to map several columns of data from one database to another where the data contains multiple spaces (once occurance of a variable number or spaces) that I need to replace with a single space. What would be the most efficient way to do this? I am using SQL2K. I was thinking a function since I know of no single Transact-SQL command that can accomplish this task.
0
1310
by: LizRickaby | last post by:
My client has several Access databased that they wanted converted into MSSQL to be able to access them online (excuse the pun). I added the first Access database, LenderPrograms, as a table in the MSSQL database with no problem, it runs fine. But when I tried to add the other Access database, LenderContacts, as another table, it doesn't work. When I first import LenderContacts using Enterprise Manager it turns all of the "text" fields into...
1
2662
by: mkepick | last post by:
migrated databases from sybase to mssql, migration exported sybase written queries written with the application build in query tool and imported to mssql databases. all db and tables migrated without errors. Regular queries without outer joins work fine but the outer join queries are failing with the following message: Microsoft ODBC SQL Server Driver SQL Server Query contain an outer-join request that is not permitted. I think the MSSQL...
11
4059
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte fs.Read(ByteArray, 0, fs.Length)
2
1371
by: =?Utf-8?B?TWFya19C?= | last post by:
I am trying to replace any occurance of minus-point "-." with minus-zero-point "-0." My regex: Regex repoint_num = new Regex(@"\-\."); My data: string xyz = "-.9"; My command: repoint_num.Replace(expr, "0."); The match is detected but the replace does not occur.
7
16018
by: j420exe | last post by:
I would like to return the first occurance of a record. The first occurance is date driven. Searched on message boards and internet and saw a few different ways to tackle this. Is using the ROWNUM = 1 the way to do it? If not, what is the recommendation solution? SELECT 'BILLING' as "Repository", o.ORDER_ID as "Order ID", O.ORDER_DATE as "Date Ordered", s.SHIP_DATE as "Date Shipped", (s.SHIP_DATE-O.ORDER_DATE) as "Lag Time" FROM...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
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
10332
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...
0
10077
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
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
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
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.