SQL Between clause with strings columns

后端 未结 6 2071
暖寄归人
暖寄归人 2020-11-27 19:24

I want to make a search using \"between\" clause over a string column. Doing some test I got this:

Let\'s assume that there is a country table with a \"name\" column

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 20:19

    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'
    

提交回复
热议问题