October 15, 2006 at 1:58 am
What's a simple way to replace a string in an NTEXT field? I found it's rather hard in Sql Server 2000. Would it be easier in SSIS? Please advise with examples. Thanks.
October 16, 2006 at 11:39 am
Well, it'll be easier in all aspects of SQL 2005, not just SSIS, because the Replace() function will work on any data type.
October 18, 2006 at 6:19 am
I'm curious. When you say you found it rather hard in SQL Server 2000, what exactly were you doing?
The Replace() function exists in SQL Server 2000 as well as 2k5, and AFAIK it's not that difficult unless you don't know what string you're searching for.
October 18, 2006 at 12:27 pm
The Replace() function doesn't work on Text and NText fields in SQL 2000. You have to use SubString() and/or Stuff() and cycle through the column or variable changing the first found instance of the pattern until they are all accounted for.
Declare
@Table Table (Myfield text not null)
Insert
Into @Table
<SELECTReplicate('1236547890', 10000)
Update
@Table
Set MyField = Replace(MyField, '654', '456')---------------------------------------------------
Msg 8116, Level 16, State 1, Line 6
Argument data type text is invalid for argument 1 of replace function.
October 18, 2006 at 12:34 pm
Okay, I see your point. I just tested it in 2k5 and got the same result when using a real Create Table / Update Table statement. Which doesn't make sense when BOL says:
"The string expression to be searched. The string_expression1 argument can be of data types that are implicitly convertible to nvarchar or ntext."
Hrm. You're right. There has to be a better way to do this. I wonder if Full Text Indexing makes these harder, different or no different.
October 18, 2006 at 12:48 pm
In SQL 2005, you should use varchar(max) and nvarchar(max) instead of Text and NText.
June 5, 2007 at 10:51 am
I know this topic is stale, but as long as your nText field is not too large, you can cast it as nvarchar and then use replace().
example
update testTable
set testNtext = replace(cast(testNtext as nvarchar(1000)),'Nmero','Número')
where testNtext like 'Nmero de Cuenta'
March 9, 2010 at 9:53 am
It may be an old thread - but thanks for that - worked a treat.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply