Wayne Barron
asked on
Access Database search for number(s) within a string of numbers and characters and return the exact number(s) searched for.
Hello, All.
In my access database column, I have the following string of numbers and characters.
The column (CASTID) has the following
I have a code to use in ASP, but what I need now, is a query within access to get the number.
So, in this case, we are needing only 2011
And looking at the string, we have two 2011's
A single 2011
And at the end, 20119
We only want, returned what is searched for.
So in this case, the only record(s) we need returned is 2011.
Is I do a search with like
Thanks.
Wayne
In my access database column, I have the following string of numbers and characters.
The column (CASTID) has the following
4807/11056/11055/11054/11053/2011/11052/11051/11050/10897/11049/20119/
I have a code to use in ASP, but what I need now, is a query within access to get the number.
So, in this case, we are needing only 2011
And looking at the string, we have two 2011's
A single 2011
And at the end, 20119
We only want, returned what is searched for.
So in this case, the only record(s) we need returned is 2011.
Is I do a search with like
SELECT CastID
FROM Media
WHERE CastID Like "*2011*"
It returns all the numbers in the row but also finds records that do not have the number associated with it.Thanks.
Wayne
ASKER
Thanks, Ryan.
Works great.
However, when I try to run it in my ASP Classic SQL Statement.
I get the following error.
Code line looks like this.
Works great.
However, when I try to run it in my ASP Classic SQL Statement.
I get the following error.
Microsoft VBScript runtime error '800a000d'
Type mismatch: '[string: "SELECT * ,IIF(INSTR("]
'
Code line looks like this.
Set getSQL = CreateObject("ADODB.Command")
getSQL.ActiveConnection=objConn
getSQL.Prepared = true
getSQL.commandtext = "SELECT * , IIF( INSTR(CASTID, '2011' )-1 > 0 , MID( CASTID, INSTR(CASTID, '2011' )-1 , 1) , "") as FrontChar, IIF( INSTR(CASTID, '2011' )+4 <= len(CASTID) , MID( CASTID, INSTR(CASTID, '2011' )+4 , 1) , "") as EndChar FROM Media Where INSTR(CASTID, '2011' ) > 0 and IIF( INSTR(CASTID, '2011' )-1 > 0 , MID( CASTID, INSTR(CASTID, '2011' )-1 , 1) , "") in ("" , "/" ) and IIF( INSTR(CASTID, '2011' )+4 <= len(CASTID) , MID( CASTID, INSTR(CASTID, '2011' )+4 , 1) , "") in ("" , "/")
set rschID = getSQL.execute
Of course, in the real code, all the numbers are replaced with ? Question marks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are the numbers always separated by /?
If the are you could try this.
If the are you could try this.
SELECT CastID
FROM Media
WHERE CastID Like "*2011/*"
@Norie,
I assume there could be values such as "2011" and "20119", so only "2011" is qualified.
I assume there could be values such as "2011" and "20119", so only "2011" is qualified.
Ryan
I think I might have misinterpreted the data, I thought this was all one value:
4807/11056/11055/11054/110 53/2011/11 052/11051/ 11050/1089 7/11049/20 119/
Mind you, if the criteria is '2011' why the need for wildcards/Like?
I think I might have misinterpreted the data, I thought this was all one value:
4807/11056/11055/11054/110
Mind you, if the criteria is '2011' why the need for wildcards/Like?
by the way, I think it could be something like this as well:
4807/11056/11055/11054/110 53/10897/1 1049/2011
so I think the presence of "/" is not compulsory
4807/11056/11055/11054/110
so I think the presence of "/" is not compulsory
ASKER
The string of data is CastID, to reference that characters ID on the Cast Table.
So, there are many cast members per video, so, I needed to add their name first, and then reference the ID number from the Media table.
So, in this case, I separated the values with the /
So, there are many cast members per video, so, I needed to add their name first, and then reference the ID number from the Media table.
So, in this case, I separated the values with the /
ASKER
Thanks again, Ryan.
Great work.
Works like a charm in both now.
Great work.
Works like a charm in both now.
You can use:
SELECT CastID
FROM Media
WHERE CastID Like "*\2011\*" OR Left([CastID], Len("2011") + 1) = "2011\" OR Right([CastID], Len("2011") + 1) = "\2011"
Wayne
So in the CastID field you do have a strings like this?
4807/11056/11055/11054/110 53/2011/11 052/11051/ 11050/1089 7/11049/20 119/
So in the CastID field you do have a strings like this?
4807/11056/11055/11054/110
How convoluted can a solution be?
ASKER
Yes, Norie.
The / separates the ID's from one another.
The / separates the ID's from one another.
ASKER
@Gustav Brock
I tested your code and did not return any results.
Running Ryan's code, returned 3 results.
I tested your code and did not return any results.
Running Ryan's code, returned 3 results.
ASKER
@Ryan.
Found an issue with the code.
I did a search for 815, and it found (8156)
So, it is not just grabbing the correct number all the time.
Found an issue with the code.
I did a search for 815, and it found (8156)
So, it is not just grabbing the correct number all the time.
I did a search for 815, and it found (8156)it's because I made an assumption to look up for an ID with 4 chars.
So, it is not just grabbing the correct number all the time.
you may try this instead:
compareValue = "815"
Set getSQL = CreateObject("ADODB.Command")
getSQL.ActiveConnection=objConn
getSQL.Prepared = true
getSQL.commandtext = "SELECT * , IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') as FrontChar, IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') as EndChar FROM Media Where INSTR(CASTID, '" & compareValue & "' ) > 0 and IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') in ('' , '/' ) and IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') in ('' , '/') "
set rschID = getSQL.execute
ASKER
The numbers can be of any size.
From the single "1"
To multiple, "1000000"
All depends on how many records are in the database.
From the single "1"
To multiple, "1000000"
All depends on how many records are in the database.
ASKER
Running it in Access, and I am getting the error.
When running in the ASP Page, it does not return any records now.
I replaced all the
'" & compareValue & "'
with the
?
Data type mismatch in criteria expression.
When running in the ASP Page, it does not return any records now.
I replaced all the
'" & compareValue & "'
with the
?
SELECT * , IIF( INSTR(CASTID, ? )-1 > 0 , MID( CASTID, INSTR(CASTID, ? )-1 , 1) , '') as FrontChar, IIF( INSTR(CASTID, ? )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, ? )+" & len(compareValue) & " , 1) , '') as EndChar FROM Media Where INSTR(CASTID, ? ) > 0 and IIF( INSTR(CASTID, ? )-1 > 0 , MID( CASTID, INSTR(CASTID, ? )-1 , 1) , '') in ('' , '/' ) and IIF( INSTR(CASTID, ? )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, ? )+" & len(compareValue) & " , 1) , '') in ('' , '/')
worked for me in Access using codes I have posted:
compareValue = "815"
Set getSQL = CreateObject("ADODB.Command")
getSQL.ActiveConnection=objConn
getSQL.Prepared = true
getSQL.commandtext = "SELECT * , IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') as FrontChar, IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') as EndChar FROM Media Where INSTR(CASTID, '" & compareValue & "' ) > 0 and IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') in ('' , '/' ) and IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') in ('' , '/') "
set rschID = getSQL.execute
db.accdb
When running in the ASP Page, it does not return any records now.
how you provide the value for ? in your codes?
ASKER
I use this.
(9 times, as there are 9 references.)
This worked on the first code you sent, that I accepted as answer.
Just not now.
(9 times, as there are 9 references.)
This worked on the first code you sent, that I accepted as answer.
Just not now.
strcastID = rs("catID") ' Feed in from another Query.
getSQL.Parameters.Append getSQL.CreateParameter("@CastID", adVarChar, adParamInput, 25, strcastID)
I don't have ASP at my working place, so I may need to test it after work ....
ASKER
OK, I got it to work in ASP.
This is weird.
The original code caught all three videos by the castID.
This new code only captures 2 of the 3.
This ID for this one is: 3915
Even tested within Access, and still, only 2 of the three records show.
This is weird.
The original code caught all three videos by the castID.
This new code only captures 2 of the 3.
This ID for this one is: 3915
Even tested within Access, and still, only 2 of the three records show.
in that case, I would suggest you to debug the SQL statement generated, and see if something we have missed out there?
...
SQL = "SELECT * , IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') as FrontChar, IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') as EndChar FROM Media Where INSTR(CASTID, '" & compareValue & "' ) > 0 and IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') in ('' , '/' ) and IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') in ('' , '/') "
response.write SQL
response.end
getSQL.commandtext = SQL
..
...
SQL = "SELECT * , IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') as FrontChar, IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') as EndChar FROM Media Where INSTR(CASTID, '" & compareValue & "' ) > 0 and IIF( INSTR(CASTID, '" & compareValue & "' )-1 > 0 , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )-1 , 1) , '') in ('' , '/' ) and IIF( INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " <= len(CASTID) , MID( CASTID, INSTR(CASTID, '" & compareValue & "' )+" & len(compareValue) & " , 1) , '') in ('' , '/') "
response.write SQL
response.end
getSQL.commandtext = SQL
..
Ah, the slashes are not backslashes. This is tested successfully:
SELECT CastID
FROM Media
WHERE CastID Like "*/2011/*" OR Left([CastID], Len("2011/")) = "2011/" OR Right([CastID], Len("/2011")) = "/2011"
ASKER
@Gustav Brock
Yours now give the correct results
@Ryan
The last code worked great.
Once I saw the len(compareValue)
I realized I had missed it, in adding the, ? Question marks.
works great.
Thanks for your assistance as always.
Great code.
Yours now give the correct results
@Ryan
The last code worked great.
Once I saw the len(compareValue)
I realized I had missed it, in adding the, ? Question marks.
works great.
Thanks for your assistance as always.
Great code.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Dale Fye
That was simple and easy.
I like this one.
One Query call is always nice.
That was simple and easy.
I like this one.
One Query call is always nice.
glad I could help.
Open in new window