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

  • Home
  • SEARCH
  • 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 8281813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:09:53+00:00 2026-06-08T10:09:53+00:00

Probably a really simple question to answer but I can’t for the life of

  • 0

Probably a really simple question to answer but I can’t for the life of me find it anywhere.

I’m looking to build select list options from the database using FOR XML Path. So far I have this:

SELECT ID AS 'option/@value', Name AS [option]
FROM MyTable
FOR XML Path('')

Which gets me this:

<option value="1">Item 1</option><option value="2">Item 2</option>... and so on

Yep pretty simple stuff but now I want to set which option is selected where ID is lets say 1. So this should only ever occur once. So I add this:

SELECT 
    ID AS 'option/@value', Name AS [option],
    CASE WHEN ID=1 THEN 'selected' ELSE '' END AS 'option/@selected',
FROM MyTable
FOR XML Path('')

Output:

<option value="1" selected="selected">Item1</option><option value="2" selected="">Item2</option>

Although this does technically set the right attribute I only need the attribute @selected on options where the case is true.

Any help or point in the right direction would be greatly received.

  • 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-08T10:09:56+00:00Added an answer on June 8, 2026 at 10:09 am

    Instead of using a empty string in your case statement return a Null

    SELECT 
        ID AS 'option/@value', 
        CASE WHEN ID=1 THEN 'selected' ELSE null END AS 'option/@selected',
        Name AS [option]    
    FROM MyTable
    FOR XML Path('')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have what is probably a really simple, studid question but I can't find
I have a simple and probably stupid question, but I can't really find any
This is probably a really dumb question with a simple answer but... I am
This is probably a really simple jQuery question, but I couldn't answer it after
This is probably a really simple question but one I've never quite worked out
This is probably something really simple but for some reason I just can't seem
I have a really simple question and probably hard answer. How to add webpartzone
there is probably a very simple answer to this very simple question, but i
This is probably a simple question but i've been unable to find a quick
There is probably a simple answer to this question but I do not have

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.