Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

UNABLE TO RETRIEVE data while using between operator Expand / Collapse
Author
Message
Posted Tuesday, February 4, 2014 11:46 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 3:18 AM
Points: 27, Visits: 96
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.
Post #1538059
Posted Wednesday, February 5, 2014 2:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:38 AM
Points: 6,861, Visits: 14,160
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1538080
Posted Thursday, February 6, 2014 1:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 3:18 AM
Points: 27, Visits: 96
WOW.Thank you Chris

it worked.
Post #1538500
Posted Thursday, February 6, 2014 8:41 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 4:53 PM
Points: 488, Visits: 1,983
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.
Post #1538701
Posted Friday, February 7, 2014 1:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:38 AM
Points: 6,861, Visits: 14,160
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1539045
Posted Friday, February 7, 2014 6:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 13, 2014 6:03 AM
Points: 30, Visits: 404
This can really reach out to bite you when dates are involved, again sort of obvious but so easy to forget/overlook
Post #1539139
Posted Friday, February 7, 2014 6:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:38 AM
Points: 6,861, Visits: 14,160
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1539148
Posted Tuesday, February 25, 2014 10:42 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 4:46 PM
Points: 157, Visits: 607
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
Post #1545059
Posted Thursday, February 27, 2014 3:41 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 21, 2014 4:09 AM
Points: 37, Visits: 142
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
Post #1545780
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse