Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Replace a string in an NTEXT field Expand / Collapse
Author
Message
Posted Sunday, October 15, 2006 1:58 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Saturday, April 4, 2009 2:47 PM
Points: 598, Visits: 25
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.


Post #315488
Posted Monday, October 16, 2006 11:39 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 10:16 AM
Points: 1,612, Visits: 1,537
Well, it'll be easier in all aspects of SQL 2005, not just SSIS, because the Replace() function will work on any data type.



My blog: SQL Soldier
Twitter: @SQLSoldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master: SQL Server 2008
Principal DBA: Outerwall, Inc.
Also available for consulting: SQL DBA Master
Post #315682
Posted Wednesday, October 18, 2006 6:19 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:34 AM
Points: 7,132, Visits: 6,293

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.

 

 



Brandie Tarvin, MCITP Database Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #316185
Posted Wednesday, October 18, 2006 12:27 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 10:16 AM
Points: 1,612, Visits: 1,537

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
Replicate('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.





My blog: SQL Soldier
Twitter: @SQLSoldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master: SQL Server 2008
Principal DBA: Outerwall, Inc.
Also available for consulting: SQL DBA Master
Post #316376
Posted Wednesday, October 18, 2006 12:34 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:34 AM
Points: 7,132, Visits: 6,293

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.

 

 



Brandie Tarvin, MCITP Database Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #316379
Posted Wednesday, October 18, 2006 12:48 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 10:16 AM
Points: 1,612, Visits: 1,537
In SQL 2005, you should use varchar(max) and nvarchar(max) instead of Text and NText.



My blog: SQL Soldier
Twitter: @SQLSoldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master: SQL Server 2008
Principal DBA: Outerwall, Inc.
Also available for consulting: SQL DBA Master
Post #316385
Posted Tuesday, June 5, 2007 10:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 19, 2014 12:52 PM
Points: 11, Visits: 260
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'
Post #371381
Posted Tuesday, March 9, 2010 9:53 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 24, 2014 10:00 AM
Points: 55, Visits: 170
It may be an old thread - but thanks for that - worked a treat.
Post #879495
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse