Solved

Extract TEXT from a STRING field separated by COMMAs....?

Posted on 2008-10-16
9
582 Views
Last Modified: 2013-11-05
Experts:

My frield looks like this:

234569,CPU,JOHN, SMITH
234570,CPU,MARY,JONES
234571,PRINTER,BILL,JENKINS
234572,CPU,TAMMY,MORRIS
234573,MONITOR,JIM,JAMES

I need to EXTRACT each piece of this STING and place it in its own column.

How can I accomplish this?

Thanks
M
0
Comment
Question by:MIKE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 1

Expert Comment

by:patricka_0377
ID: 22735821
if its a csv (comma separated variables)? just create a file call it filename.csv open it up in excel it will do the rest for you
0
 
LVL 17

Author Comment

by:MIKE
ID: 22735844
NO...no.....no.
   
  : )

This is a field in my DATATABLE in SQL Server 2005.

I need to use the separate pieces in OTHER logic, so first of all, I need to know HOW to create a Stored Proc and/or Function that will extract each piece so that I can go to my next step and use the pieces AS IF they are Columns of data.

Thanks
M
0
 
LVL 1

Expert Comment

by:patricka_0377
ID: 22735845
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 17

Author Comment

by:MIKE
ID: 22735868
For example, a start would be HOW do I get this from this field:

CPU
CPU
PRINTER
CPU
MONITOR

This would be the SECOND section of data,...right after the first Comma.

Thx
M
0
 
LVL 17

Author Comment

by:MIKE
ID: 22735886
I'm doing THIS to get the first piece:

select LEFT(myField,6) as [OrderID]
from myTable

The aboev SQL gives me this:

234569
234570
234571
234572
234573

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22735983
Try this.  I think this is the right syntax for the second field.

select substring(MyField, charindex(',',myfield)+1, charindex(',',myfield, charindex(',',myfield)+1)-1)
from MyTable
0
 
LVL 17

Author Comment

by:MIKE
ID: 22736075
Almost....

Returns this:

CPU,JOHN,
CPU,MARY,
PRINTER,BILL,J
CPU,TAMMY
MONITOR,JIM,J

So... it is returning the correct STARTING like CPU and PRINTER, but it also returns the NEXT 6 characters...

I only need what is between the 1st and 2nd commas...

Thx
M
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22736858
Here you go:

Just replace your table name and remove everything after the from.  I just threw your example into a derived table for testing.
select substring(myfield, charindex(',',myfield)+1,charindex(',',myfield,charindex(',',myfield)+1)-(charindex(',',myfield)+1))
 
from 
(select '234569,CPU,JOHN, SMITH' as myfield
union select '234570,CPU,MARY,JONES'
union select '234571,PRINTER,BILL,JENKINS'
union select '234572,CPU,TAMMY,MORRIS'
union select '234573,MONITOR,JIM,JAMES'
) a

Open in new window

0
 
LVL 17

Author Closing Comment

by:MIKE
ID: 31506931
THANKS
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question