On 15 Mar 2005 14:47:56 -0800, hharry wrote:
Thanks all,
Yes, the issue is slightly more complex and I have went with this
option:
SELECT 'D or Shorty', SUBSTRING('D or Shorty', 1, CHARINDEX(' or ', 'D
or Shorty') - 1) + ' ; ' +
SUBSTRING('D or Shorty', (CHARINDEX(' or ', 'D or Shorty') + 4), LEN('D
or Shorty'))
If your issue will come up in a similar way in the future, it might be
worth it to encapsulate that "replace first occurence only" into a UDF.
CREATE FUNCTION ReplaceFirstOccurence
( @src varchar(8000), @sub varchar(8000), @repl varchar(8000) )
RETURNS varchar(8000)
AS
BEGIN
DECLARE @n int
SET @n = CHARINDEX(@sub, @src)
RETURN SUBSTRING(@src, 1, @n-1)
+ @repl
+ SUBSTRING(@src, @n + LEN(@sub) + 1,8000)
END
A much crazier idea that I just discovered, while googling on this topic,
is to pull in the VBScript regular expression library!
http://www.sqlteam.com/item.asp?ItemID=13947
They only demonstrate using it for testing a string against a regexp, but
it could easily be extended to use replace.