SQL Between clause with strings columns SQL Between clause with strings columns postgresql postgresql

SQL Between clause with strings columns


The expression

name between 'A' and 'B'

is equivalent to

name>='A' and name<='B'

So 'Argentina' is >='A' and <='B' and it satisfies the condition. But 'Bolivia' is NOT <='B'. 'Bolivia'>'B'. It doesn't just look at the first letter: it looks at the whole string. Which is surely the way it ought to be: if it didn't do this, there'd be no way to say that you wanted a range that included 'Smith' but not 'Smithers'.

To accomplish what you want, you could say:

substr(name,1,1) between 'A' and 'B'

or:

name like 'A%' or name like 'B%'

or:

name>='A' and name<'C'


i think i know how to solve your problem. u can try adding extra character in the back like this

select * from tablea where column1 between 'ABC' and 'ACD'+'Z'

this will return a result from ABC% to ACE


Another query that would get countries that start with b as well as A, would be:

Select * from country where name between 'a' and 'c'