Solved

SUBSTRING

Posted on 2009-04-01
9
448 Views
Last Modified: 2012-05-06
Hi Everybody,

I'm facing problem in writing a sql query that will select a sub-string in a string. There is a column in table called description and its data type is nvarchar(50). It holds values like

XXX,C700,3/4,XXXXXXXX,1 CU.FT.
XXX,C700,1 1/2,XXXXXXXX,10 CU.FT.

and I need to select the size part of the string, for eg 3/4 or 1 1/2. I see pattern in the values, the size part always comes after the second , (comma) in the string. Can you please suggest me a sql that will select only that part.

Thanks
0
Comment
Question by:Samoin
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24041512
declare @DAta nvarchar(50)
set @data=N'XXX,C700,1 1/2,XXXXXXXX,10 CU.FT.'

select substring(substring(@data, charindex(',', @data,charindex(',', @data)+1)+1,100), 1, charindex(',',substring(@data, charindex(',', @data,charindex(',', @data)+1)+1,100))-1)
0
 
LVL 1

Author Comment

by:Samoin
ID: 24041613
Well the data in the column is not fixed, sometimes the value is like

ABB,PSMT,5/8x3/4,PULSER,10 GAL.
ABB,5UM20,3/4,PULSER,GAL,HOT WATER,1 GAL.
Neptune,T-10,1 1/2,PULSEGEN,6DIGIT,1000 GAL.
Neptune T-10 3/4 ARB-V 4 Digit 1000 Gal

So initializing a particular value won't help I guess.
0
 
LVL 12

Expert Comment

by:udaya kumar laligondla
ID: 24041619
I am assuming that the first two fields are of 3 and 4 chars and the second comma is always the 9th
declare @Test nvarchar(500)
set @Test=N'XXX,C700,1 1/2,XXXXXXXX,10 CU.FT.'

select substring(@Test, charindex(',', @Test,9)+1,charindex(',',@Test,charindex(',', @Test,9)+1)-charindex(',', @Test,9)-1)
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 12

Expert Comment

by:udaya kumar laligondla
ID: 24041648
use
select substring(substring(description, charindex(',', description,charindex(',', description)+1)+1,100), 1, charindex(',',substring(description, charindex(',', description,charindex(',', description)+1)+1,100))-1) from tablename
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24043513
My example was just a sample.  Let's say your field is called SomeField and your table is called SomeTable.

Your query would then be:

And uday, I would appreciate you at least say "i'm copying Brandon's code" when you post an update ;).
select substring(substring(SomeField, charindex(',', SomeField,charindex(',', SomeField)+1)+1,100), 1, charindex(',',substring(SomeField, charindex(',', SomeField,charindex(',', SomeField)+1)+1,100))-1)
from SomeTable

Open in new window

0
 
LVL 1

Author Comment

by:Samoin
ID: 24044165
Uday and Brandon,

I keep getting this error

Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.
0
 
LVL 1

Author Comment

by:Samoin
ID: 24044198
Got it,

I did a small change in the query

select substring(substring([description], charindex(',', [description],charindex(',', [description])+1)+1,100), 1, charindex(',',substring([description], charindex(',', [description],charindex(',', [description])+1)+1,100)))
from [TableName]
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 24044251
uday copied my script and just changed the field names from my "sample @Data".  My script showed you what to do.  I think that I have earned this.
0
 
LVL 1

Author Closing Comment

by:Samoin
ID: 31565425
Fair Enough, and I think I have assigned points according to the effort but again I will say it was a combined effort and major contribution was yours.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 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