Solved

How do I strip a number from a string

Posted on 2011-09-23
19
217 Views
Last Modified: 2012-05-12
I have a field named Process in my table.  In the field is text: Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLEY.  What would be the sql to be used in a query to strip the 15 out of the string.  The number could be any anything (7 or 300 or 12 or 1000, etc.)
0
Comment
Question by:LoriLHS
[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
  • 8
  • 5
  • 4
19 Comments
 
LVL 75
ID: 36590030
Is there any pattern to that number before and/or after ... like those two dashes?

Like Obstetrics, Gynecology-- 100% of 9999-- RANCHO CUCAMONGA  :-)

MX
0
 

Author Comment

by:LoriLHS
ID: 36590045
yes. '% of'  before and '--' after
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590070
try:

declare @p as varchar(100)='Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLE';
select SUBSTRING(@p, charindex('% of',@p)+5, charindex('--',@p, charindex('% of',@p))-charindex('% of',@p)-5),

-->

15
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590077
oops, is that access :) sorry, solution is for SQL
0
 
LVL 75
ID: 36590091
Example:;

Left("Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLEY",instrRev("Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLEY","--")-3) & Mid("Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLEY",instrRev("Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLEY","--"))

Returns this.
Obstetrics, Gynecology-- 100% of -- ANTELOPE VALLEY

Is that what you want?

mx
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590101
in Access we use InStr and parameters reversed and use Mid instead of SubStr

0
 

Author Comment

by:LoriLHS
ID: 36590116
Sorry, I just want the 15 only.  The field is called Process, wouldn't that be used in the sql instead of the 'Obstetrics, Gynecology-- 100% of 15-- ANTELOPE VALLEY'
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590130
access version

select mid(
Process ,
instr(Process ,'% of')+5,
instr(mid(Process ,instr(Process ,'% of')+5, len(Process )),'--')-1
) from myTable
0
 
LVL 75
ID: 36590143
So, in a query ... to display w/o :

SELECT Left([FIELDX],InStrRev([FIELDX],"--")-3) & Mid([FIELDX],InStrRev([FIELDX],"--")) AS Expr1
FROM Table1
WHERE (((Table1.FIELDX) Is Not Null));

mx
0
 
LVL 75
ID: 36590154
"wouldn't that be used in the sql instead of the "

You want to retain the 15?  You implied strip it out ...

mx
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 36590177
If you just want the numbers to remain, try thisL

SELECT Val(Mid([FIELDX],InStr([FIELDX],"% of ")+5)) AS Expr1
FROM Table1
WHERE (((Table1.FIELDX) Is Not Null));


mx
0
 
LVL 75
ID: 36590199


 What would be the sql to be used in a query to strip the 15 out of the string

Also, if Process  can be Null, the Accepted Solution will error out.

mx
0
 

Author Comment

by:LoriLHS
ID: 36590239
DatabaseMX, you are right.  I thought I could fix the error out.  You deserved the points, sorry.
0
 
LVL 75
ID: 36590258
You can hit the Request Attention button and re assign.

thx.mx
0
 

Author Comment

by:LoriLHS
ID: 36590277
I cannot find the request attention button...
0
 
LVL 75
ID: 36590308
see image

Are you in the ANTELOPE VALLEY ?

mx
Capture1.gif
0
 

Author Comment

by:LoriLHS
ID: 36590329
Done. Thank you.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

717 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