Replace a string in an NTEXT field

  • 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.

  • 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[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

  • 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 AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • 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.


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

  • 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 AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • In SQL 2005, you should use varchar(max) and nvarchar(max) instead of Text and NText.


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

  • 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'

  • 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