从名称范围中选择

时间:2013-08-17 17:10:58

标签: sql-server tsql select filter sql-server-2008-r2

例如,我在数据库中有这个名字

LastName
Amy
Betty
Camy
Daisy
David
Johnson

只有在想要从Amy选择到David时才有效:

select * from Student
where LastName >= 'A' and LastName <= char(ascii('D') + 1)
order by LastName  

如果我只想选择从Amy到Daisy,我将如何做?

1 个答案:

答案 0 :(得分:2)

(我可能遗漏了你忘记在你的问题中包含的内容,但是)为什么要让生活变得更加艰难:

SELECT *
FROM Student
WHERE LastName BETWEEN 'Amy' AND 'Daisy'
ORDER BY LastName

SQL Fiddle


要回复评论,您可以采取以下方式:

SELECT *
FROM Student
WHERE LastName >= 'Amy'
AND (LastName <= 'Dai' OR LastName LIKE 'Dai%')

SQL Fiddle证明这仍然会导致单个索引搜索。