Solved

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

Posted on 2008-10-16
9
581 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

734 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