【问题标题】:select between with value range在值范围之间进行选择
【发布时间】:2018-08-14 02:25:31
【问题描述】:
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

如果我将186cf2564 的范围指定为187sa5635,它应该会显示:

186cf2564
186cf2564
187sa5635 

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

【问题讨论】:

  • 显示你的 SQL 查询你在尝试什么
  • 你总是在`itemcode`中有前导3个数字
  • @ShivShankarNamdev 请关闭您的 cmets 中的大写字母,表示大喊
  • 不,它混合了字母数字,它可能是 ICNF1 到 ICNF7 我们需要取最后一个数字
  • 您必须使用in 运算符搜索字符串,或者您可以使用rightleft 函数

标签: sql sql-server sql-server-2008 sql-server-2005


【解决方案1】:

试试这个 -

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

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

【讨论】:

    【解决方案2】:

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

    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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多