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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:26:35+00:00 2026-05-25T03:26:35+00:00

I need your help. Sadly I don’t know SQL as well as I do

  • 0

I need your help. Sadly I don’t know SQL as well as I do C# (or most other languages) and I’ve hit my limits with this query. This post may be a bit verbose, so I apologize for that in advance, but I want to make sure I include all the necessary info.

My goal is to create a query that selects data from SQL, groups it by a substring value of one of the columns, and outputs in XML. I’m pretty close, but I’ve hit a wall.

Here’s an example of what it should look like:

<EXAMPLE_DATA>
  <headEnd nam="AAAA">
    <hardware fromDevice="ExampleDeviceAAAA" />
    <hardware fromDevice="ExampleDeviceAAAA" />
    <hardware fromDevice="ExampleDeviceAAAA" />
  </headEnd>
  <headEnd nam="BBBB">
    <hardware fromDevice="ExampleDeviceBBBB" />
    <hardware fromDevice="ExampleDeviceBBBB" />
    <hardware fromDevice="ExampleDeviceBBBB" />
</EXAMPLE_DATA>

As you can see, the last four characters of the fromDevice are what define my headEnd groupings… here’s what I’m able to return right now:

<EXAMPLE_DATA>
  <headEnd nam="[headendId]">
    <hardware fromDevice="ExampleDeviceAAAA" headendId="AAAA" />
    <hardware fromDevice="ExampleDeviceAAAA" headendId="AAAA" />
    <hardware fromDevice="ExampleDeviceAAAA" headendId="AAAA" />
    <hardware fromDevice="ExampleDeviceBBBB" headendId="BBBB" />
    <hardware fromDevice="ExampleDeviceBBBB" headendId="BBBB" />
    <hardware fromDevice="ExampleDeviceBBBB" headendId="BBBB" />
</EXAMPLE_DATA>

And finally, here’s the code I have that returns the XML above:

SELECT 
  '[headendID]' as "@nam"
  , (
    SELECT hardware.Name as "@fromDevice", RIGHT(hardware.Name, 4) as "@headendId"
    FROM tblHardware AS hardware
    GROUP BY RIGHT(hardware.Name, 4), hardware.Name
    for xml path ('hardware') , type
  ) 
for xml path ('headEnd'), root ('EXAMPLE_DATA')

I’ve removed a lot of non-essential columns to try and make this post a bit easier to read.

So, looking at what I need the XML to look like, is it even possible? I guess anything’s possible… but in this case I’m completely stumped.

Thanks for reading!

EDIT: To make sure my question is clear, what I need is for the SQL code to output XML data grouped by the substring query of hardware.Name. I’m trying to make the output look like the first XML block above.

  • 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-05-25T03:26:35+00:00Added an answer on May 25, 2026 at 3:26 am

    Here you go:

     SELECT 
       RIGHT(categ.Name, 4) as "@nam" ,    
       (
       SELECT hardware.Name as "@fromDevice"
       FROM tblHardware AS hardware
       WHERE RIGHT(categ.Name, 4) = RIGHT(hardware.Name, 4)
       for xml path ('hardware') , type
       )
     FROM tblHardware as categ
     GROUP BY  RIGHT(categ.Name, 4)
     for xml path ('headEnd'), root ('EXAMPLE_DATA')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need your help for this. I am relatively new to programming and
ive being trying to cope with this problem but still. I need your help.
I need your help on this guys!!!! During the upload of the files to
Need your help, trying to change this script: https://github.com/MrHus/jquery-monthly-ical To load external JSON data
I need your help, i'm stacked on this project. When i run my applicaion
I need your help. I need to create live wallpaper like this : Bikini
I need your help in this case: I have: 1 11 111 Cat1 a,b,c
I need your help !!!! I want to connect to sql server from a
Kindly need your help as this really taking me long time to try. From
i need your help in access db i have a table like this ID

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.