Updating a date in XML with xquery

  • I have an XML untyped column in my table with data like this:

    <Appointment/>

    <Date>1/14/2008</Date>

    </Appointment>

    I'd like to add 1 year to the date through a query, but am having a hard time formulating the xquery to do so.

    Thanks for your help!

    Jay

  • Look up "modify() method" in Books Online. It has directions on updating XML data.

    Edit: Actually, just took another look at it, and it's pretty sparse. Check out the sample XML applications on MSDN for better data.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Here is on option:

    DECLARE @x XML

    SELECT @x = '

    [Appointment]

    [Date]1/14/2008[/Date]

    [/Appointment]'

    DECLARE @d VARCHAR(10)

    SELECT @d = CONVERT( VARCHAR,

    DATEADD(year, 1, @x.value('(Appointment/Date)[1]','DATETIME')),

    101)

    SET @x.modify('

    replace value of (Appointment/Date/text())[1]

    with sql:variable("@d")

    ')

    SELECT @x

    /*

    [Appointment]

    [Date]1/14/2008[/Date]

    [/Appointment]

    */

    I have posted a collection of tutorials on common XQuery tasks here:

    http://blog.sqlserver.me/2008/06/xquery-labs-collection-of-xquery-sample.html

    .

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

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