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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:50:08+00:00 2026-05-20T00:50:08+00:00

I have no control over the format of the data I am trying to

  • 0

I have no control over the format of the data I am trying to process. I could, of course, use a scripting language to deal with the following problem outside of the database, but I would like to avoid that because of the amount of data I am dealing with and because I’d like to eliminate the necessity of manual steps.

In short, I have a table of lists. A list might consist of a single 3-digit string, more than one 3-digit strings, a range of 3-digit strings, e.g. 012-018, or a number of 3-digit strings and ranges of 3-digit strings. For example:

drop table list;
drop table lists;

create table lists (id varchar, vals varchar);

insert into lists values('A', '001,003-005');
insert into lists values('B', '008-007');
insert into lists values('C', '010, 011, 012');
insert into lists values('D', '011-013, 016-018, 020');

I know, I know.

I would like to turn this into the following table:

create table list (id varchar, val varchar);
A   001
A   003
A   004
A   005
B   008
B   007
C   010
C   011
C   012
D   011
D   012
D   013
D   016
D   017
D   018
D   020

Is there any way to do this in SQL?

  • 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-20T00:50:09+00:00Added an answer on May 20, 2026 at 12:50 am

    Since you haven’t tagged your question with a specific RDBMS, I’ll have to answer generally.

    SQL itself doesn’t provide the basic operation that you’re looking for, which is basically a string split. This means that you’ll have to write your own, or use one of the many that have been published online.

    You’ve complicated matters a bit, though, with the ranges that you have in your data. This means that your procedure is going to look something like this:

    1. Insert your data into a temp/memory table and iterate over it procedurally (or, alternatively, use a cursor to do the same)
    2. For each record in your set, extract the non-normalized string data and split it by ','.
    3. For each element within the split data, you’ll have to then split that by '-' (which, for non-range elements, should return you a single result).
    4. If your second split (on '-') yields one result, it’s a single record that you can insert into your final destination. If it yields two results, then it’s a range and you’ll have to iterate from the start to the finish (using elements 1 and 2 of that split) and insert records into your final destination

    Edit after comment

    Unfortunately, I don’t have any familiarity with PROC SQL or SAS, so I can’t provide a specific solution for that. I can post something below in SQL Server T-SQL, which should hopefully get you started.

    declare @results table (idx int identity(1, 1), id varchar(5), data varchar(max))
    declare @elements table (idx int identity(1, 1), element varchar(25))
    declare @range table (idx int identity(1, 1), element varchar(25))
    
    insert into @results (id, data)
    select
        your_id,
        your_data
    
    from your_source
    
    declare @i int
    declare @cnt int
    
    declare @j int
    declare @cnt2 int
    
    declare @element varchar(25)
    
    declare @first int
    declare @second int
    
    declare @start int
    declare @end int
    
    declare @id varchar(5)
    declare @data varchar(max)
    
    select @i = min(idx) - 1, @cnt = max(idx) from @results
    
    while @i < @cnt
    begin
        select @i = @i + 1
    
        select @id = id, @data = data from @results where idx = @i
    
        delete @elements
    
        insert into @elements (element) 
        select
            element
    
        from split(@data, ',')
    
        select @j = min(idx) - 1, @cnt2 = max(idx) from @elements
    
        while @j < @cnt2 
        begin
            select @j = @j + 1
    
            select @element = element from @elements where idx = @j
    
            delete @range
    
            insert into @range (element)
            select
                element
    
            from split(@element, '-')
    
            select @first = min(idx), @second = max(idx) from @range
    
            if @first = @second --single element
                insert into final_destination (id, value)
                select
                    @id,
                    element
    
                from @range
            else if @second - @first = 1 -- two elements, as desired
            begin
                select @start = convert(int, element) - 1 from @range where idx = @first
                select @end = convert(int, element) from @range where idx = @second
    
                while @start < @end
                begin
                    select @start = @start + 1
    
                    insert into final_destination (id, value)
                    values (@id, @start)
                end
            end
            else -- error condition, bad input
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read that I can use Python's Format Specification Mini-Language to have more control
I have no control over database schema and have the following (simplified) table structure:
I have no control over how the data is saved in this table. However,
A form I don't have any control over is POSTing data to my PHP
Some code that I don't have control over is overriding the global JSON object
First, I'll start off by saying that I do not have control over the
Assuming I have no control over the content in the iframe, is there any
I have a server that I have no control over, it's JSON based and
I have a varchar field that looks like (sadly I have no control over
I have a Sql Database (which I have no control over the schema) that

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.