简体   繁体   English

Microsoft SQL之间的字符声明不包括在内吗?

[英]Microsoft SQL between statement for characters is not inclusive?

I am trying to select all values that have a first name beginning with the letters ad, however when I do this 我正在尝试选择所有以字母ad开头的名字的值,但是当我这样做时

select * from tblprofile where firstname between 'a' and 'd'

I get all values from a to c, not including d, how can I make sure it includes d? 我得到了从a到c的所有值,不包括d,如何确保它包含d?

It is inclusive. 它具有包容性。

You don't get the results you want because any string beginning with 'd' and longer than 1 character is greater than 'd'. 您没有得到想要的结果,因为任何以'd'开头且长度超过1个字符的字符串都大于'd'。 For example 'da' > 'd' . 例如'da' > 'd' So, your query would return all values starting with 'a', 'b', 'c', and a value 'd'. 因此,您的查询将返回以“ a”,“ b”,“ c”和值“ d”开头的所有值。

To get the results you want use 获得想要使用的结果

select * from tblprofile where firstname >= 'a' and firstname < 'e'

Try using Left() Function: 尝试使用Left()函数:

SELECT *
FROM tblprofile
WHERE LEFT(FirstName,1) between 'a' and 'd'

Another way is using a union select like this 另一种方法是使用像这样的联合选择

SELECT * FROM tblprofile WHERE LEFT(FirstName,1) = 'a' union SELECT * FROM tblprofile WHERE LEFT(FirstName,1) = 'b' union SELECT * FROM tblprofile WHERE LEFT(FirstName,1) = 'c' union SELECT * FROM tblprofile WHERE LEFT(FirstName,1) = 'z' SELECT * FROM tblprofile WHERE LEFT(FirstName,1)='a'联合SELECT * FROM tblprofile WHERE LEFT(FirstName,1)='b'联合SELECT * FROM tblprofile WHERE LEFT(FirstName,1)='c'联合SELECT *从tblprofile的WHERE LEFT(FirstName,1)='z'

The advantage of using union is if you need to get the results stating with A, K and X, strings out of sequence. 使用并集的好处是,如果您需要获得以A,K和X字符串顺序说明的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM