SQL在整数字段上按条件选择

时间:2012-06-26 08:40:49

标签: sql select conditional-statements

我的表中有一个整数列。它是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

的行

请帮忙

5 个答案:

答案 0 :(得分:4)

这样就足够了:

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

答案 1 :(得分:1)

          Select field from table where substr(field,,) = value

答案 2 :(得分:1)

SELECT * FROM YOURTABLE

WHERE 

substr(PRODUCT_ID, 3, 2)='33'
AND
substr(PRODUCT_ID, 1, 2)='11'

OR

SELECT * FROM YOURTABLE

WHERE 

PRODUCT_ID LIKE '11%33%'

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

reference of substr

<强>目的

SUBSTR函数返回一部分char,从字符位置开始,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参数传递的浮点数自动转换为整数。

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

你需要使用_通配符字符 -

SELECT * 
FROM TABLE
WHERE
FIELD LIKE '1133_____'

这里,每个_用于一个字符。因此,您需要使用相同数量的_来保持长度相同