I am trying to search a database in a not exact manner and know that I can use the like statement to help with this search of the database but do not know exactally how to do this. Any help?
Printable View
I am trying to search a database in a not exact manner and know that I can use the like statement to help with this search of the database but do not know exactally how to do this. Any help?
Try:
SELECT * FROM TABLE_NAME WHERE COLUMN_NAME LIKE '*CRITERIA*'
TABLE_NAME is name of the table that you want to extract the data. Insert your table name here.
COLUMN_NAME is the name of the column. Insert your column name here.
* is a wild card character CRITERIA is the data you want and another * wildcard at the end.
For example: Say my table is named CONTACTS and the column is called NAME(contains first and last separated by a space) then I would use:
SELECT * FROM CONTACTS WHERE NAME LIKE 'John*'
This will return all names beginning with John, the last name is the wildcard.
Psyrus
The wildcards depend on what Database you are running the Query on.
If you are using SQL Server then the wildcards are :
% - (Percent) Match none or more characters
_ - (Underscore) Match EXACTLY one character
LIKE '%ed' will find : (ends in)
fred
bed
LIKE '%ed%' will find : (contains)
fred
bed
medical
LIKE '_ed' will find : (3 letter word, any 1st letter)
bed
ted
wed
...or something similar. Psyrus answer is good if you want to extract all records from a Table which match a criteria for processing. The above code is good if you are seeking a single instance.Code:sName = "John"
rs.movefirst
Do
rs.FindNext("Name Like '*" & sName & "*'")
If rs.EOF Then
MsgBox "Damn got that one Wrong"
Exit Do
End If
'
' Is this the record you want sort of testing
'
Exit Do
Loop
Hope this Helps
Thanks for your help Psyrus, Gen-X, and Jethro. I went with the approach Gen X submitted. I forgot to mention that it was a SQL database. Hope I am able to get the same levle of quality responses in the future.
Nixoid