Solved

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

Posted on 2008-10-16
9
578 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

773 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