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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:53:30+00:00 2026-05-28T01:53:30+00:00

I have a tough programming problem in SQL Server 2000 that I’m hoping I

  • 0

I have a tough programming problem in SQL Server 2000 that I’m hoping I can get help with. I am a novice with T-SQL (I mostly use MySQL) so maybe someone could look at this and see an easy way to do it.

I have a table with lots of columns that contain either a 0 or 1. For example, I have columns labeled var1, var2, var5, var9. etc. up to about 120 columns total. Notice that the suffixes on these columns aren’t necessarily consecutive. Each of these columns contain a 0 or 1. What I need to do is create a new column the contains the literal column names of every column that contains a 1.

For example, my data look like this:

ID var1   var2   var5    var7   var9
--------------------------------------
1    0      1      0      0       1
2    0      1      1      1       0

I need a new column that contains the values

ID  NewCol
---------------------
1   var2,var9
2   var2,var5,var7

and so on.

For reasons I won’t go in to, I can’t use any client side code like VB, Perl, etc. I have to do this in a stored procedure. Due to the large number of columns that could change in either name or number, I need something other than a hardcoded series of SELECT CASE statements.

I feel really limited by my T-SQL knowledge. Can this be done??

TIA

  • 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-28T01:53:31+00:00Added an answer on May 28, 2026 at 1:53 am

    You can do this with a query that looks like this:

    select ID,
           stuff(
           case var1 when 1 then ',var1' else '' end +
           case var2 when 1 then ',var2' else '' end +
           case var5 when 1 then ',var5' else '' end +
           case var7 when 1 then ',var7' else '' end +
           case var9 when 1 then ',var9' else '' end
           , 1, 1, '')
    from YourTable
    

    Here is a way to build it dynamically.

    declare @SQL varchar(8000)
    set @SQL = ''
    
    select @SQL = @SQL + ' + case '+COLUMN_NAME+' when 1 then '','+COLUMN_NAME+''' else '''' end'
    from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = 'YourTable' and
          COLUMN_NAME <> 'ID'
    
    set @SQL = 'select ID, stuff('+stuff(@SQL, 1, 3, '')+', 1, 1, '''') from YourTable'
    
    exec(@SQL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one's a tough one - I have a JFrame that generates JTextFields. When
I am a computer sciences student and I usually have really tough programming assignments.
I'm new to web programming and I have the following form that I wish
I have been facing the above mentioned problem in my java application that I
I'm currently looking at a simple programming problem that might be fun to optimize
OK, I don't do much .Net programming, but I do have one that I
I'm still new to C# programming and have been able to get WebDriver working
I'm currently programming an app that deals with line graphs. I have three dots
I'm still a graphics programming novice and bet the following problem is just a
I've been programming Java for 2 years now, and I have encountered a problem

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.