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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:54:41+00:00 2026-05-12T08:54:41+00:00

This is a very standard newbie question, but I am looking for an expert

  • 0

This is a very standard newbie question, but I am looking for an expert clever way to do this with little code. (I know how to do this long hand, with procedural code and cursors, so we can skip that part of the answer.) Basically, I have a de-normalized data set for which I need to extract a normalized table in a set processing way. The raw data comes from Excel, which I attach as a table to query from MS SQL. A single field (in Excel and therefore SQL) has a comma separated list of departments that each product participates.

I know that I should normalize the data, but it is coming from Excel (and it is not practical for the user to do so). I know I can do this with a procedure to loop through each record (like a cursor or with statement) but that seems unnecessary with the tools available today. Is there a single statement where I can extract a primary key with a “participates” field for these records?

Think of the source as:

ProductIDDepartments
 123      1,3,5,15  
 456      2,4,5,16  

I did the normal select * from dbo.split( CommaSepField) but this only works on one record at a time (and you cannot provide a field’s value in with a table valued function. Not to mention, how do you union all these single result sets?

Unionizing all the result sets got me thinking about Common Table Expressions, but I cannot quite figure out how to (1) get the primary key in with the expanded fields, and (2) how to format the query to work.

The output of course would be:

ProductIDDept
 123      1  
 123      3  
 123      5  
 123      15 
 456      2  
 456      4  
 456      5  
 456      16 

I researched this quite a bit in StackOverflow and Google, but didn’t really find an answer that could apply. I apologize if I missed it. Please send links :D.

  • 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-12T08:54:41+00:00Added an answer on May 12, 2026 at 8:54 am

    You can actually use your comma separation TVF, by joining using the CROSS APPLY operator:

    select * from Table CROSS APPLY  dbo.split(CommaSepField)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I fear this is a very trivial question. But I'm having some trouble getting
This is a very simple question, but embarrassingly enough I am not sure how
I can write my own but I'm guessing this is very standard. Is there
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
In this very simple code example, messages get lost every once in a while.
I'm using this very handy plugin to sort elements. But comparing elements on a
I am trying to send an email using the following very standard code. However,
Apologies if this has been asked before but I really didn't know what to
In knockout.js I have a very standard field that looks like this: <label data-bind=text:
Say I have very standard mysql connection code: $dbhost = '192.168.1.99'; $dbuser = 'dbuser';

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.