AlphaPress Publishing
P.O. Box 5942 Albany, NY 12205
Tel: 518-250-3890
Email: alphapress@hotmail.com  


  
Click to buy this book from Amazon

Section Menu 

News

Pindaro is now working on a comprehensive book on SQL Server 2014.  For any comments you can email the author at alphapress@hotmail.com.

  

 

 

Use LIKE to find blank spaces in fields

To contact the author click here

This article is from the book "Access 2007 Pure SQL

To download the sample database click here


Use LIKE to test a field for the presence of spaces 

Check for the presence of blank spaces in the city field

Discussion:

The same supervisor comes back and says she needs a report of customers from certain cities.  This time you are wise enough to know that to get the right results you need to check for spaces first.  Then however, you reason that some city names do contain spaces.  For example, New York contains a space and Los Angeles contains a space.  In addition, you realize that the trim() function is good enough for leading and trailing spaces.  What if you have a space right in the middle of the word?  You can actually look for spaces using the LIKE operator with the * wildcard character.  In this example, we are looking for two spaces in any part of the field value.  To achieve this we use the * three times.  You can test for one space using two asterisks ("* *").  We found two city names with two blank spaces as you can see in the figure below.    

Code:

SELECT lastname, firstname, city, orderdate

FROM tbls_CustomerOrders

WHERE (city LIKE "* * *") 

Result: