Solved

SUBSTRING

Posted on 2009-04-01
9
406 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now