On 18 Aug 2004 07:19:13 -0700, Rathtap wrote:
This query returns only those starting with A and B. Why?
SELECT name FROM mytable WHERE name BETWEEN 'A' AND 'C'
Hi Rathap,
Because the name 'Chris' is considered to be greater than 'C'. If the
first character is equal, comparison looks at the second, etc; for this
purpose, the shorter name is padded with spaces. So the comparison of
'Chris' to 'C' in detail: 'C" gets padded to 'C '; the first characters
(both 'C') are equal; 'h' is compared to ' ' and is greater.
Remember that myname BETWEEN 'A' AND 'C' is shorthand for myname >= 'A'
AND myname <= 'C', so all names beginning with A or B will be selected, as
well as names exactly equal to 'C'.
To get all names beginnen with A, B and C, use BETWEEN 'A' AND 'CZZZZZZZ'
(and check how numbers and special characters sort in your collation), use
myname >= 'A' and myname < 'D', or use myname LIKE '[ABC]%'.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)