Trailing Spaces Significant with the LIKE Keyword

ID: Q118803

1.50 1.51 Windows kbprg

The information in this article applies to:

  The Microsoft Foundation Classes, included with:

    - Microsoft Visual C++ for Windows, versions 1.5 and 1.51

SUMMARY

The Microsoft Desktop ODBC drivers provided with Visual C++ versions 1.50 and 1.51 (the Simba driver) store trailing spaces in fixed-length text/character columns. This is important when using the LIKE keyword for filter clauses because trailing spaces are significant in LIKE. Therefore, to ensure pattern matching is successful against identifiers stored in fixed-length columns, a percent sign (%) should be added to the end of the search string.

MORE INFORMATION

Below are some examples of how to use the LIKE keyword in the m_strFilter string of a CRecordset object when using the ODBC drivers included with Visual C++. The two wildcard characters you can use are the percent sign (%) and the underscore (_). The percent sign can represent any number of characters. The underscore (_) can represent a single character.

Sample Code

   // This filter retrieves all records that start with "John".
   // Records such as "John", "Johnny", etc. will be returned.
   // Note that the % is necessary to retrieve "John" because
   // of the trailing spaces mentioned above.

   m_strFilter="Name LIKE 'John%'"

   // This filter retrieves only records where the field starts
   // with "J" and is followed by an additional character. Note
   // that a space is required after the underscore (_) to ensure that
   // only records with two characters are found. Using "J_" isn't
   // enough to find a record where the Name field is "Jo", for
   // example.

   m_strFilter="Name LIKE 'J_ %'"

Additional reference words: kbinf 1.50 1.51 2.50 2.51 KBCategory: kbprg KBSubcategory: MfcDatabase
Keywords          : kbDatabase kbMFC kbODBC kbVC 
Version           : 1.50 1.51
Platform          : WINDOWS

Last Reviewed: July 11, 1997