Solved

SUBSTRING

Posted on 2009-04-01
9
415 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
  • 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:udayakumarlm
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
 
LVL 12

Expert Comment

by:udayakumarlm
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

932 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

14 Experts available now in Live!

Get 1:1 Help Now