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 7838425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:06:04+00:00 2026-06-02T15:06:04+00:00

I have a XML column in a table that looks like this: <word A=al

  • 0

I have a XML column in a table that looks like this:

<word A="al"   B="h"   C="Ps" />
<word A="has"  B="es"  C="Pf" />
<word A="mom"  B="es"  C="Ph" />

I need to convert into table like this:

 word  | A  | B  | C
 ====================
   al  | A1 | B1 | C1
   has | A2 | B2 | C2
   mom | A3 | B2 | C3

I want to do by a function in SQL Server.

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-02T15:06:06+00:00Added an answer on June 2, 2026 at 3:06 pm

    Guessing on data type here, and also guessing that you didn’t mean to convert a1 -> A1, etc.

    DECLARE @x TABLE(XMLFIELD XML);
    
    INSERT @x SELECT '<word A="a1"  B="b1"  C="c1" />'
    UNION ALL SELECT '<word A="a2"  B="b2"  C="c2" />'
    UNION ALL SELECT '<word A="a3"  B="b2"  C="c3" />';
    
    -- INSERT INTO dbo.OtherTable
    SELECT 
      A = x.a.value('@A', 'varchar(32)'),
      B = x.a.value('@B', 'varchar(32)'),
      C = x.a.value('@C', 'varchar(32)')
    FROM @x AS src
    CROSS APPLY src.XMLFIELD.nodes('word') AS x(a);
    

    Results:

    A       B       C
    ------- ------- -------
    a1      b1      c1
    a2      b2      c2
    a3      b2      c3
    

    EDIT

    And now that you’ve completely changed the question, making my answer look insane and completely unrelated to your question, let’s try again. Once more I’m making some guesses here because you’re not describing your logic or requirements very well. Does the B column increment every time you see a new value? Same with the C column? Is there any chance there are duplicates that aren’t in consecutive rows?

    DECLARE @x TABLE(XMLFIELD XML);
    
    INSERT @x SELECT '<word A="al"   B="h"   C="Ps" />'
    UNION ALL SELECT '<word A="has"  B="es"  C="Pf" />'
    UNION ALL SELECT '<word A="mom"  B="es"  C="Ph" />';
    
    ;WITH y AS
    (
      SELECT 
        word = x.a.value('@A', 'varchar(32)'),
        n = ROW_NUMBER() OVER (ORDER BY x.a.value('@A', 'varchar(32)')),
        B = x.a.value('@B', 'varchar(32)'),
        C = x.a.value('@C', 'varchar(32)')
      FROM @x AS src
      CROSS APPLY src.XMLFIELD.nodes('word') AS x(a)
    )
    SELECT word, 
        A = 'A' + RTRIM(n),
        B = 'B' + RTRIM((SELECT COUNT(*)+1 FROM y AS y2 WHERE n < y.n AND B <> y.B)),
        C = 'C' + RTRIM((SELECT COUNT(*)+1 FROM y AS y2 WHERE n < y.n AND C <> y.C))
    FROM y 
    ORDER BY word;
    

    Results:

    word   A    B    C
    ------ ---- ---- ----
    al     A1   B1   C1
    has    A2   B2   C2
    mom    A3   B2   C3
    

    If you want coherent answers that actually solve your problem, you should work on describing your problem well (and getting it right the first time, before a bunch of effort is spent trying to solve the wrong problem).

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

Sidebar

Related Questions

I have a XML column in a table that looks like this: <word A=al
I have mysql table that has a column that stores xml as a string.
I have an XML valued column that has multiple parent nodes. I need to
I have an XML column in a table; I want to promote a certain
I have this XML in a column in my table: <keywords> <keyword name=First Name
I have an xml column which contain data like this: <AuthorList CompleteYN=Y> <Author ValidYN=Y>
I have a heavily nested XML document that I need to load into my
I have a table create table Profiles (id int, xml_data xml ) that has
I have a stored proc that inserts XML into an underlying table. Unfortunately the
I have a general query that looks like this: SELECT DISTINCT pb.id, pb.last, pb.first,

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.