UNABLE TO RETRIEVE data while using between operator

  • Hi Experts,

    I came across a weird issue this morning

    while using Between operator am unable to retrieve data that starts with second option,

    example am using

    select * from USER where vendor between 'N' and 'V'

    here i expect to get all the records that has vendor name starting with N,O,P,......U,V

    but to my surprise i found am just getting N,O ......,U. am unable to retrieve vendors start with 'V'

    when i use between 'N' and 'W' it shows with 'V' also........but not showing vendors with 'W'

    so i doubt whether between is considering second option 'V' or not.

    Help me with this

    Thank you.

  • The BETWEEN range ends at 'V'. If you have a vendor with exactly this value then it will be included. Vendors called 'V' + anything else will be excluded because they sort after 'V', outside your range. As an experiment, try BETWEEN 'N' AND 'W', which will return all the vendors beginning with 'V' and any vendors which are just 'W'.

    There's another way to collect all the vendors beginning with 'V', and that's to follow it with a character which would sort after the letters, something like BETWEEN 'N' AND 'V'+CHAR(255).

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • WOW.Thank you Chris

    it worked.

  • ChrisM@Work (2/5/2014)


    The BETWEEN range ends at 'V'. If you have a vendor with exactly this value then it will be included. Vendors called 'V' + anything else will be excluded because they sort after 'V', outside your range. As an experiment, try BETWEEN 'N' AND 'W', which will return all the vendors beginning with 'V' and any vendors which are just 'W'.

    There's another way to collect all the vendors beginning with 'V', and that's to follow it with a character which would sort after the letters, something like BETWEEN 'N' AND 'V'+CHAR(255).

    This is one of those painfully obvious things that I'm embarrassed I didn't know. Thanks, Chris.

  • Thanks. There are tons of these little bitty helpful tricks and you will find a good many are covered in Spackle articles by Jeff Moden.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • This can really reach out to bite you when dates are involved, again sort of obvious but so easy to forget/overlook

  • batesview (2/7/2014)


    This can really reach out to bite you when dates are involved, again sort of obvious but so easy to forget/overlook

    It certainly can - but Jeff Moden has most date issues covered in his Spackle articles too. It doesn't matter how much you think you know, there's always going to be something you've missed. Many of these articles have "added value" (Bingle "lagniappe" to see what I mean) making them very worth while reading material even if you are comfortable with the main topic.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I actually didn't know you could use BETWEEN in this way, for (var)char datatypes. And without the wildcard % operator. Groovy.


    "If I had been drinking out of that toilet, I might have been killed." -Ace Ventura

  • Good question and awesome answer by experts.. we should not use between operator in string comparision.. if we then should know the limitation that it will not consider last character string because it could start VA,VB like that and it will be greater than V

    i think between is perfect for date data type

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply