REPLACE

  • I have a char column with some misc text I want to get rid of.

    I am using SELECT REPLACE, etc...

    which produces the desired output, but doesn't update the actual table.

    dumb q, but how do i get the actual table column updated?

    thx

    f

  • update table_name

    set col1 = replace(col1,'XXX','')

    are you using something like this?

    Regards
    Durai Nagarajan

  • I would add that you can also do it like this:

    --sample data

    SELECT * INTO #test

    FROM (VALUES (1,'blah AAA'),(2,'bbb bbb bbb'),(3,'AAA ccc')) t(id,val);

    SELECT * FROM #test;

    WITH your_table AS

    (

    SELECT * FROM #test WHERE val like '%AAA%'

    )

    UPDATE your_table

    SET val=REPLACE(val,'AAA','[xxx]')

    SELECT *

    FROM #test

    I like this technique because, in SQL Server Management Studio, you can highlight & execute just the SELECT * FROM #test WHERE val like '%AAA%' portion of the statement to see what rows are going to be updated.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply