Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SUBSTRING

Posted on 2009-04-01
9
Medium Priority
?
457 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 1000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

636 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