How to extract the values from a comma separated list?

Experts,

I have a list of values in my database separated by commas: example; 1, 2, 3, 4

I need query syntax to extract this data so that I can use each value.

I've searched for a while but, can't seem to find a simple example that I can build upon.  

Any suggestion?  Thanks Experts.
evibesmusicAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

answer_dudeCommented:
Here's something that should get you started... this code assumes exactly three data values separated by commas... but it can easily be expanded to infinite/unknown numbers...
...

        sVal1 = ""
        sVal2 = ""
        sVal3 = ""
        For x = 1 To Len(rs.Fields(1))
        
            nTo = InStr(x, rs.Fields(1), ",")
            If nTo = 0 Then nTo = Len(rs.Fields(1))
            If sVal1 = "" Then
                sVal1 = Mid$(rs.Fields(1), x, nTo - x)
            ElseIf sVal2 = "" Then
                sVal2 = Mid$(rs.Fields(1), x, nTo - x)
            ElseIf sVal3 = "" Then
                sVal3 = Mid$(rs.Fields(1), x, nTo - x)
            End If
            
            x = nTo
            If x >= Len(rs.Fields(1)) Then
                Exit For
            End If
                    
        Next x
...

Open in new window

0
MaartenHensenCommented:
Or you can do:
$datafromdb;
$data = explode(",", $datafromdb);

// and then you can work with all the values... nomather how many there are...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SheilsCommented:
Use a query similar to the one in the attached file

dbcomma.mdb
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

evibesmusicAuthor Commented:
@MaartenHensen:

Using your example: I am running the following code.  I am continually getting an 'array' when trying to print the exploded result.  Any thoughts?
$get_prefs = "SELECT * FROM permmed_prefs";
		$prefs_data = mysql_query($get_prefs) or die ("Could not run query: " . $get_prefs . "<br />\n" . mysql_error () );
		while($datafromdb = mysql_fetch_array($prefs_data)){
			echo $data = explode(",", $datafromdb['Department']); 
			echo '<br /><br />';
		}

Open in new window

0
NerdsOfTechTechnology ScientistCommented:
print_r
$get_prefs = "SELECT * FROM permmed_prefs";
$prefs_data = mysql_query($get_prefs) or die ("Could not run query: " . $get_prefs . "<br />\n" . mysql_error () );
while($datafromdb = mysql_fetch_array($prefs_data)){
 $data = explode(",", $datafromdb['Department']); 
 print_r ($data);
 echo '<br /><br />';
}

Open in new window

0
NerdsOfTechTechnology ScientistCommented:
or you can foreach the output from the explosion:
$get_prefs = "SELECT * FROM permmed_prefs";
$prefs_data = mysql_query($get_prefs) or die ("Could not run query: " . $get_prefs . "<br />\n" . mysql_error () );
while($datafromdb = mysql_fetch_array($prefs_data)){
 $data = explode(",", $datafromdb['Department']); 
 foreach ($data as $value){
  echo $value;
  echo '<br />';
 }
 echo '<br /><br />';
}

Open in new window

0
evibesmusicAuthor Commented:
@NerdsOfTech & MaartenHensen:

Thank you both for your assistance with this solution.  This was my solution:

$get_prefs = "SELECT * FROM permmed_prefs";
$prefs_data = mysql_query($get_prefs) or die ("Could not run query: " . $get_prefs . "<br />\n" . mysql_error () );
      while($datafromdb = mysql_fetch_array($prefs_data)){
            $data = explode(",", $datafromdb['Departments']);
            foreach ($data as $value){
            echo $value;
            echo '<br />';
            }
      echo '<br /><br />';
      }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.