Access "like" statement (Full Version)

FrontPage Talk - Great Talk About Microsoft FrontP: http://www.frontpagetalk.com/forums/
- Taking FrontPage Further: http://www.frontpagetalk.com/forums/default.asp?catApp=3
- - FrontPage and Databases: http://www.frontpagetalk.com/forums/appid_3/tt.htm



Message


comoaa -> Access "like" statement (7/5/2008 9:04:04 AM)

For several years I wrote Access Query's using the like statement,
IIF(lastname like "T*" lastname, "",would filter out all last names starting with "T". Still works in Access but since my Web Host upgraded his Windows server software it does not work on the web site. Like used to be a handy code. What happened? Howe come it now works in the Access database but not on the web in a DBR?

Thanks




TexasWebDevelopers -> RE: Access "like" statement (7/6/2008 9:08:48 PM)

http://www.texaswebdevelopers.com/examples/sql_intro.asp

The LIKE condition allows you to use wildcards in the where clause of an SQL statement. This allows you to perform pattern matching. The LIKE condition can be used in any valid SQL statement - select, insert, update, or delete.

The patterns that you can choose from are:

% allows you to match any string of any length (including zero length)

_ allows you to match on a single character

Here are a few patterns:
'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there are two characters between A and Z instead of one).
'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition.
'%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition.
'%AN%': All string that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both satisfy the condition.
Examples using % wildcard

The first example that we'll take a look at involves using % in the where clause of a select statement. We are going to try to find all of the suppliers whose name begins with 'Hew'.

SELECT * FROM supplier
WHERE supplier_name like 'Hew%';

You can also using the wildcard multiple times within the same string. For example,

SELECT * FROM supplier
WHERE supplier_name like '%bob%';

In this example, we are looking for all suppliers whose name contains the characters 'bob'.

You could also use the LIKE condition to find suppliers whose name does not start with 'T'. For example,

SELECT * FROM supplier
WHERE supplier_name not like 'T%';

By placing the not keyword in front of the LIKE condition, you are able to retrieve all suppliers whose name does not start with 'T'.


Examples using _ wildcard
Next, let's explain how the _ wildcard works. Remember that the _ is looking for only one character.

For example,

SELECT * FROM supplier
WHERE supplier_name like 'Sm_th';

This SQL statement would return all suppliers whose name is 5 characters long, where the first two characters is 'Sm' and the last two characters is 'th'. For example, it could return suppliers whose name is 'Smith', 'Smyth', 'Smath', 'Smeth', etc.

Older versions of Microsoft Access use an * instead of a %, and a ? for it's _

Here is another example,

SELECT * FROM supplier
WHERE account_number like '12317_';

You might find that you are looking for an account number, but you only have 5 of the 6 digits. The example above, would retrieve potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return suppliers whose account numbers are:

123170
123171
123172
123173
123174
123175
123176
123177
123178
123179


Examples using Escape Characters
Next, in Oracle, let's say you wanted to search for a % or a _ character in a LIKE condition. You can do this using an Escape character.

Please note that you can define an escape character as a single character (length of 1) ONLY.

For example,

SELECT * FROM supplier
WHERE supplier_name LIKE '!%' escape '!';

This SQL statement identifies the ! character as an escape character. This statement will return all suppliers whose name is %.

Here is another more complicated example:

SELECT * FROM supplier
WHERE supplier_name LIKE 'H%!%' escape '!';

This example returns all suppliers whose name starts with H and ends in %. For example, it would return a value such as 'Hello%'.

You can also use the Escape character with the _ character. For example,

SELECT * FROM supplier
WHERE supplier_name LIKE 'H%!_' escape '!';

This example returns all suppliers whose name starts with H and ends in _. For example, it would return a value such as 'Hello_'.




comoaa -> RE: Access "like" statement (7/7/2008 6:52:53 AM)

Thank you for this complete assessment of the Like condition! I used it quite often in the past for DBR views on my non-profit web sites. It appears that the main point is that "*" has been changed to "%". Star still works fine in Access Query's. About two years ago it just stopped working on the web! No error reports, it just didn't work any more! Difficult to understand where the problem was coming from since the query would work just fine in Access!

Thank you so much for your help. I will go give it a test now. Writing around it has produced some long conditional statements in some areas of my query's. Ive gotten good at the IIF/and/or statements!

Tom Thomas




TexasWebDevelopers -> RE: Access "like" statement (7/7/2008 8:02:09 AM)

Yep--Access as a stand-alone db application and Access as a web database are two TOTALLY different animals. You have to unlearn one to learn the other.




comoaa -> RE: Access "like" statement (7/13/2008 1:40:23 PM)

If I use like "T*" the query runs fine in Access but does not work on the web. If I change to like "T%" the query will not run in Access but works fine on the website. This GOOFY!! I have both Web DBR's and Access Reports that I want to run off the same query. Do I have to write duplicate Query's to do this, one with % signs and one with *?

Access is a Microsoft product, Windows for servers is a Microsoft product. Why don't the gears mesh?? I am running a 2004 model of Access. I wonder if upgrading to the 2007 model would take out the kinks?

Thanks,

Tom Thomas




TexasWebDevelopers -> RE: Access "like" statement (7/13/2008 8:10:05 PM)

I don't know if you are using double quotes for emphasis fror this post or if you used them in the code. Single quotes are needed in the code.




comoaa -> RE: Access "like" statement (7/14/2008 11:15:22 AM)

My version of Access (2002) requires that query's use double quotes! I just tried it with single quotes and the query will not work. The "Help" area also says to use double quotes around modifiers for Like, = ect. So does the Access 2000 Bible reference book I use. The double quoted modifiers work in all the FrontPage DBR's I have written.

What version of Access are you using and are you using FrontPage insert command to insert a DBR. I'm really confused now because what your saying is NOT what I'm seeing, either in Access itself nor in Web FrontPage DBR's.

Tom Thomas
Bend, Oregon




TexasWebDevelopers -> RE: Access "like" statement (7/14/2008 4:45:27 PM)

Sorry If I confused you. Depending on whether the variable is a numeral or string the quotes vary.
For instance, this sql statement to select a home with x bedrooms and x baths with a range of sq.ft. works just fine:
sql = "SELECT * FROM Model_tbl WHERE (xmodel_name='" & xmodel_name & "' AND xbed=" & xbed & " AND xbath=" & xbath & " AND xsqft=" & xsqft & " AND xden=" & xden & " AND xmodel_number='" & xmodel_number & "' );"

This is also a working sql statement:

sql = "Select Distinct Attorneys.AttorneyID, Attorneys.PersID, AttorneyStatusID, FirstName, LastName, Phone, Email, MiddleInitial from Attorneys" & _
PAClause & LocationClause & " Where (FirstName LIKE '" & FirstName & "%' " & _
"OR Alias1 LIKE '" & FirstName & "%' " & _
"OR Alias2 LIKE '" & FirstName & "%' " & _
"OR Alias3 LIKE '" & FirstName & "%') " & _
"AND LastName LIKE '" & LastName & "%' " & _
PositionSubClause & PASubClause & LocationIDSubClause & _
" AND isEmployed = True " & _
"ORDER BY LastName, FirstName ;"


It really depends on how you've written your query and how the database columns are formatted...
Can you post code?




comoaa -> RE: Access "like" statement (7/14/2008 5:20:28 PM)

I see, You re writing code for SQL. I am using the Insert Database results command in FrontPage. My code here is for the little blue box found in the upper left corner of http://COMOAA.com

<!--webbot bot="DatabaseRegionStart" endspan i-checksum="3888" --><tr>
<td><font size="2">
<!--webbot bot="DatabaseResultColumn" s-columnnames="CountOfID,Service" s-column="Service" b-tableformat="TRUE" b-hashtml="FALSE" b-makelink="FALSE" clientside b-MenuFormat preview="<font size="-1">&lt;&lt;</font>Service<font size="-1">&gt;&gt;</font>" startspan --><%=FP_FieldVal(fp_rs,"Service")%><!--webbot bot="DatabaseResultColumn" endspan i-checksum="13069" --></font></td>
<td><font size="2">
<!--webbot bot="DatabaseResultColumn" s-columnnames="CountOfID,Service" s-column="CountOfID" b-tableformat="TRUE" b-hashtml="FALSE" b-makelink="FALSE" clientside b-MenuFormat preview="<font size="-1">&lt;&lt;</font>CountOfID<font size="-1">&gt;&gt;</font>" startspan --><%=FP_FieldVal(fp_rs,"CountOfID")%><!--webbot bot="DatabaseResultColumn" endspan i-checksum="16105" --></font></td>
</tr>
<!--webbot bot="DatabaseRegionEnd" b-tableformat="TRUE" b-menuformat="FALSE" u-dbrgn2="_fpclass/fpdbrgn2.inc" i-groupsize="0" clientside tag="TBODY" preview="<tr><td colspan=64 bgcolor="#FFFF00" width="100%"><font color="#000000">This is the end of a Database Results region.</font></td></tr>" startspan --><!--#include file="_fpclass/fpdbrgn2.inc"-->
<!--webbot bot="DatabaseRegionEnd" endspan i-checksum="62730" -->

Tom Thomas
Bend, Oregon




TexasWebDevelopers -> RE: Access "like" statement (7/15/2008 6:41:31 AM)

I posted sql statements written against Access Databases--just like you are doing.
Strip all of the comment tags (anything in between <!-- and -->) away from the code you gave (above) and this is what you have posted:
Service <%=FP_FieldVal(fp_rs,"Service")%>
CountOfID <%=FP_FieldVal(fp_rs,"CountOfID")%>
This is just a database call for these two fields. What is the sql statement you are using and how is the db structured?




Page: [1]

Valid CSS!




Forum Software powered by ASP Playground Advanced Edition 2.0.5
Copyright © 2000 - 2003 ASPPlayground.NET