0

I have a Customers Database in MS Access. I want to make a sql query that searches a substring from my primary key field.

For Example :

I want to select all the IDs that contain the number "35".

My DataBase before the SELECT statement :

enter image description here

My DataBase after the SELECT statement :

Example For a wanted substring

Is it possible ?

1
  • but.. is your primary key a string or a number?? If it is a string you should check this link out: w3schools.com/sql/sql_like.asp Commented May 5, 2017 at 20:15

3 Answers 3

2

Since the Access wildcard is * and not % and, AFAIK, Access doesn't support CONTAINS I think you really want:

SELECT * FROM MyTable WHERE ID like "*35*"

Though based on our discussion in comments, other options (for when the ID field isn't a string) are:

SELECT * FROM MyTable WHERE Str(ID) like "*35*"

And

SELECT * FROM MyTable WHERE CStr(ID) like "*35*"
Sign up to request clarification or add additional context in comments.

8 Comments

can i use it in C# using OleDb?
I've never tried, but why not give it a go? If you run into problems, come back with another question.
It's working for string field , how can i use it for integer field ?
Within Access it seemed to work for me against integer fields without changes. However, you might want to try WHERE str(ID) like "*35*".
Thank You , But i need it for C# and it doesn't work with str(ID) , It doesn't know what is str.
|
0

Assuming that the ID field is numeric,

In MsAccess:

Select * from myTable
where str(ID) like "*35*"

in SQL Server

Select * from myTable
where str(ID, 12, 0) like '%35%'

1 Comment

can i use it in C# using OleDb?
-1
SELECT *
FROM your_table
WHERE CAST(ID AS VARCHAR(20)) LIKE '%35%';

2 Comments

I didn't downvote, but question was about MS Access. I don't think your code is valid there. For starters, the wildcard is * not %. As for the CAST ... ?
Ah, I didn't notice the MS Access tag. I jumped the gun there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.