+ Reply to Thread
Results 1 to 5 of 5

SQL LIKE case sensitivity issue

  1. #1
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Thumbs down SQL LIKE case sensitivity issue

    I have an Excel 2007 (Win 7) utility worksheet that uses VBA/SQL to extract data from a table located on a separate Excel data worksheet. The code works fine whether or not the data worksheet is open in Excel, except for one thing (at least so far). If I use LIKE in the SQL (e.g. WHERE city_prov LIKE '%NS%') :
    - if the data worksheet is open, this expression is case sensitive
    - if the data worksheet is not open, the expression is case insensitive
    So I can get completely different results depending on whether or not the data worksheet is open when I run the query. Does this make sense to anyone?

    I want the LIKE to be case sensitive. I can ensure that this happens by adding code to open the data worksheet before the query is implemented. However, I would be curious to know why there should be any difference in the way the SQL is interpreted in these two cases.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: SQL LIKE case sensitivity issue

    Hello keyston,

    Welcome to the Forum!

    You can convert all text to either upper case or lower case and then select the data. SQL has the statements Ucase and LCase, just like VBA. You could try something like this...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: SQL LIKE case sensitivity issue

    Using Ucase would be fine if I wanted to ensure case insensitivity, but I want the opposite. I want the LIKE to be case sensitive. I want to pick up those cases where city_prov includes NS (e.g. Halifax, NS) but not ns (e.g. Saint Johns, NL). If the data worksheet is open, this is what I get. But I can't understand why the exact same SQL LIKE code is case insensitive if the data worksheet being queried is not open.

  4. #4
    Registered User
    Join Date
    07-27-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: SQL LIKE case sensitivity issue

    Hi Keyston,

    That's not one I've come across before and sounds like a bug, but why don't you put in some code to open the source workbook in the background? That way you can guarantee the same results for all workbooks.

    Cheers,
    Dom

  5. #5
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: SQL LIKE case sensitivity issue

    Quote Originally Posted by Dom83 View Post
    Hi Keyston,

    That's not one I've come across before and sounds like a bug, but why don't you put in some code to open the source workbook in the background? That way you can guarantee the same results for all workbooks.

    Cheers,
    Dom
    My original post indicates that this is exactly what I have done - but the mystery remains! If it is a bug, it may be fixed in Excel 2010.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1