Solved

How do I strip a number from a string

Posted on 2011-09-23
19
212 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
  • 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 51

Expert Comment

by:HainKurt
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
 
LVL 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
ID: 36590130
access version

select mid(
Process ,
instr(Process ,'% of')+5,
instr(mid(Process ,instr(Process ,'% of')+5, len(Process )),'--')-1
) from myTable
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 - Access MVP) 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

758 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

21 Experts available now in Live!

Get 1:1 Help Now