Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:05:44+00:00 2026-06-14T23:05:44+00:00

I have some data stored in an SQL Server XML data column in a

  • 0

I have some data stored in an SQL Server XML data column in a table. It’s used to configure HTML content (to be sent as emails) and works fine. However, I would now like to change some of the data, but I can’t seem to find the correct incantation to effect just the bits I want.

Take the following as an example.

declare @s1 varchar(max);
declare @s2 varchar(max);
declare @s3 varchar(max);

-- rough approximation of my data
set @s1 = '<email sender="bob"><html><body><table><tbody><tr><td><b>'
set @s2 = '</b></td></tr><tr><td><b>'
set @s3 = '</b></td></tr></tbody></table></body></html></email>'

declare @t table (id int, data xml)

insert into @t values (1, @s1 + 'Hello World' + @s2 + 'Goodbye cruel world' + @s3)
insert into @t values (2, @s1 + 'Hello World' + @s2 + 'I''m leaving you today' + @s3)
insert into @t values (3, @s1 + 'Hello World' + @s2 + 'Goodbye, goodbye, goodbye' + @s3)

select data from @t

update @t -- change sender to "fred"
set data.modify('replace value of (/email/@sender)[1] with "fred"')

select data from @t

select data,x.nd.value('b[1]','varchar(max)') -- find the "hello world" bits
from @t
cross apply data.nodes('email/html/body/table/tbody/tr/td') as x(nd)
where x.nd.value('b[1]','varchar(max)') = 'Hello World'

I want to change the “sender” data (which I think I’ve worked out), and the “Hello World” data – which I can find, but I don’t know how to change. I’m not really sure what’s going on with the “cross apply” syntax either, and guess that I could write the XPath stuff to only find the “Hello World” nodes without the “where” clause ?

My table only has about 9 rows in it, so I could write nine update commands if needed and this is a one-off data maintenance task.

Any help would be appreciated.

Edit:

Is this the way its done ?

update @t
set data.modify('replace value of (email/html/body/table/tbody/tr/td/b/text())[1] with "bonjour monde"')
from @t
cross apply data.nodes('email/html/body/table/tbody/tr/td') as x(nd)
where x.nd.value('b[1]','varchar(max)') = 'Hello World'

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:05:45+00:00Added an answer on June 14, 2026 at 11:05 pm

    Almost 🙂 Your example from the Edit updates the first email/html/body/table/tbody/tr/td/b but the WHERE clause looks for any email/html/body/table/tbody/tr/td. So if you have an email that has, say, two tr and the second one matches the filter you still update the first one. Eg.

     insert into @t values (4, '<email sender="bob"><html><body><table><tbody>
       <tr><td><b>Ciao mondo!</b></td></tr><tr><td><b>Goodbye, goodbye, goodbye</b></td></tr>
       <tr><td><b>Hello World</b></td></tr><tr><td><b>Can''t get rid of me!</b></td></tr>
       </tbody></table></body></html></email>');
    

    The xml above will not be updated correctly: it will change the first tr which does not actually matches the filter, and it will leave the second one unaffected although is the one that should had been changed. There are several ways to approach this. Personally I would make the XPath/XQuery in the .modify do the work for me. I would also critique the use of cross apply when the XML.exist would suffice:

    update @t
    set data.modify('replace value of (email/html/body/table/tbody/tr/td/b[.="Hello World"]/text())[1] with "bonjour monde"')
    from @t
    where data.exist('email/html/body/table/tbody/tr/td/b[.="Hello World"]') = 1;
    

    This will be more correct, but it must be run repeatedly as only one XML element at a time can be modified (ie. if two tr have Hello World then the modify will only update the first one).

    Probably all these points are academic for you, since your first update likely did the job. But for future reference. BTW, learn how XPath filters work, they are tremendously helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML data stored in a varchar(max) column on SQL Server 2005.
Setup: I have some xml data stored in a database (SQL Server 2008 R2)
I have created a SQL Server 2000 stored procedure to return some XML data
I have some xml data stored in an XML Column in a table in
I have some varbinary data stored in a table in MS Sql Server 2005.
I am reading some data stored in Xml format in SQL Server database and
I have some (untyped) XML being stored in SQL Server 2005 that I need
I have some xml data stored in database (sqlserver) in following format: eg. email
I have a SQL Server DB table that has a column ReceivedDate defined as
I have address data stored in an sql server 2000 database, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.