Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like everybody has said, this is a pain to do natively in SQL Server. If you <strong>must</strong> then I think this is the proper approach.</p> <p>First determine your rules for parsing the string, then break down the process into well-defined and understood problems.<br> Based on your example, I think this is the process: <br></p> <ol> <li>Separate comma separated values in the string into rows</li> <li>If the data <strong>does not</strong> contain a dash, then it's finished (it's a standalone value)</li> <li>If it <strong>does</strong> contain a dash, parse the left and right sides of the dash</li> <li>Given the left and right sides (the range) determine all the values between them into rows</li> </ol> <p>I would create a temp table to populate the parsing results into which needs two columns: <br> <code>SourceRowID INT, ContainedValue INT</code> </p> <p>and another to use for intermediate processing: <br> <code>SourceRowID INT, ContainedValues VARCHAR</code></p> <p>Parse your comma-separated values into their own rows using a CTE like this <strong>Step 1 is now a well-defined and understood problem to solve</strong>:</p> <p><a href="https://stackoverflow.com/questions/5493510/turning-a-comma-separated-string-into-individual-rows">Turning a Comma Separated string into individual rows</a></p> <p>So your result from the source <br> <code>'1-2,5'</code><br> will be: <br> <code>'1-2'</code><br> <code>'5'</code> </p> <p>From there, <code>SELECT</code> from that processing table where the field <strong>does not</strong> contain a dash. <strong>Step 2 is now a well-defined and understood problem to solve</strong> These are standalone numbers and can go straight into the results temp table. The results table should also get the ID reference to the original row.</p> <p>Next would be to parse the values to the left and right of the dash using <code>CHARINDEX</code> to locate it, then the appropriate <code>LEFT</code> and <code>RIGHT</code> functions as needed. This will give you the starting and ending value.</p> <p>Here is a relevant question for accomplishing this <strong>step 3 is now a well-defined and understood problem to solve</strong>:</p> <p><a href="https://stackoverflow.com/questions/10921400/t-sql-substring-separating-first-and-last-name">T-SQL substring - separating first and last name</a></p> <p>Now you have separated the starting and ending values. Use another function which can explode this range. <strong>Step 4 is now a well-defined and understood problem to solve</strong>:</p> <p><a href="https://stackoverflow.com/questions/12762883/sql-create-sequential-list-of-numbers-from-various-starting-points">SQL: create sequential list of numbers from various starting points</a></p> <p><a href="https://stackoverflow.com/questions/4182054/select-all-n-between-min-and-max">SELECT all N between @min and @max</a></p> <p><a href="https://stackoverflow.com/questions/1393951/what-is-the-best-way-to-create-and-populate-a-numbers-table">What is the best way to create and populate a numbers table?</a></p> <p>and, also, insert it into the temp table.</p> <p>Now what you should have is a temp table with every value in the exploded range.</p> <p>Simply <code>JOIN</code> that to the other table on the values now, then to your source table on the ID reference and you're there.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload