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?
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:
','
.'-'
(which, for non-range elements, should return you a single result).'-'
) 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 destinationEdit 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.