选择值范围

问题描述 投票:0回答:2
select * from purchase_items where item_code between '180' and '186'

我有一个表purchase_items,例如,我有以下值。

item_code
180
182
183
1852563
186cf2564
186cf2564
187sa5635

如果我在180187之间选择,则显示所有值。

但我需要180183之间的范围它应该显示以下内容:

180
182
183

如果我将范围186cf2564187sa5635它应该显示:

186cf2564
186cf2564
187sa5635 

只有如何让它显示正确的输出?

sql sql-server sql-server-2008 sql-server-2005
2个回答
0
投票

从示例数据中,您可以选择字符串的数字部分。它更多的是硬编码工作,如果您的数据一致,则此查询有效

select * from purchase_items where left( item_code,3) between '180' and '186'

用于选择行ICNF1 to ICNF7

  select * from purchase_items where Right( item_code,1) between 1 and 7

对于186cf2564到187sa5635

select * from purchase_items where left( item_code,3) between '186' and '187'

0
投票

试试这个 -

select * from purchase_items where item_code between '180' and '183'

select * from purchase_items where LEFT(item_code,3) between '186' and '187'
© www.soinside.com 2019 - 2024. All rights reserved.