Solved

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

Posted on 2008-10-16
9
577 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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculated columns 13 61
VB6 ListBox Question 4 32
MSSQL: Replace text (typo) 7 30
How to get time difference in minutes and seconds only between 2 dates 2 19
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now