【问题标题】:SQL Select by condition on a integer fieldSQL Select by condition on a integer field
【发布时间】:2012-06-27 13:13:49
【问题描述】:

我的表中有一个整数列。它是product id,具有类似

的值
112233001  
112233002  
113311001  
225577001  

这个编号(AABBCCDDD)由4部分组成:

AA : first level category  
BB : second level category  
CC : third level category  
DDD : counter  

我想检查 SELECT 语句中的条件以选择例如 BB = 33 和 AA = 11 的行

请帮忙

【问题讨论】:

标签: sql select conditional-statements


【解决方案1】:
          Select field from table where substr(field,,) = value

【讨论】:

    【解决方案2】:

    这样就够了:

    select x from table where field >= 113300000 and field < 113400000 
    

    【讨论】:

      【解决方案3】:
      SELECT * FROM YOURTABLE
      
      WHERE 
      
      substr(PRODUCT_ID, 3, 2)='33'
      AND
      substr(PRODUCT_ID, 1, 2)='11'
      

      SELECT * FROM YOURTABLE
      
      WHERE 
      
      PRODUCT_ID LIKE '11%33%'
      

      是的,简而言之,您必须转换为字符串

      reference of substr

      目的

      SUBSTR 函数返回字符的一部分,从字符位置开始,substring_length 个字符长。 SUBSTR 使用输入字符集定义的字符计算长度。 SUBSTRB 使用字节而不是字符。 SUBSTRC 使用 Unicode 完整字符。 SUBSTR2 使用 UCS2 代码点。 SUBSTR4 使用 UCS4 代码点。

      If position is 0, then it is treated as 1.
      
      If position is positive, then Oracle Database counts from the beginning of char to find the first character.
      
      If position is negative, then Oracle counts backward from the end of char.
      
      If substring_length is omitted, then Oracle returns all characters to the end of char. If substring_length is less than 1, then Oracle returns null.
      

      char 可以是任何数据类型CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB。 position 和 substring_length must be of datatype NUMBER,或任何可以隐式转换为 NUMBER 的数据类型,并且必须解析为整数。返回值与 char 的数据类型相同。作为参数传递给 SUBSTR 的浮点数会自动转换为整数。

      【讨论】:

      • 因为 '11' 和 '13' 是例子,我不能使用第二个 Select 语句,但第一个正是我想要的。谢谢!
      • 是否需要将我的 product_id 转换为字符串?或者substr,可以帮我做吗?
      【解决方案4】:

      这似乎可行。否则,您可能必须将它们转换为字符串并解析出您需要的值,这会使您的查询变得更慢。

      SELECT * 
          FROM table t
      WHERE t.field >= 113300000
      AND t.field < 113400000
      

      【讨论】:

        【解决方案5】:

        你需要使用_ 通配符-

        SELECT * 
        FROM TABLE
        WHERE
        FIELD LIKE '1133_____'
        

        这里,每个_ 代表一个字符。所以你需要放相同数量的_来保持长度不变

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-04
          • 2018-12-31
          • 2011-04-13
          • 1970-01-01
          • 2022-12-02
          • 2016-03-05
          相关资源
          最近更新 更多