简体   繁体   English

Oracle SQL正则表达式以匹配Varchar列-视为数字

[英]Oracle SQL Regular expression to match Varchar column - treat as numbers

I have a table with a column whose data type is VARCHAR2(7 Char). 我有一个表,该表的列的数据类型为VARCHAR2(7 Char)。 The data in this column has values such as 1006,1007,2002 etc 此列中的数据具有诸如1006、1007、2002等的值

I would like a regular expression that matches 4002,4003,4005,4011,4013 but NOT 4001. 我想要一个匹配4002、4003、4005、4011、4013但不匹配4001的正则表达式。

First i tried using To_number(columnName) > 4001 but got an ORA-01722 invalid number error. 首先,我尝试使用To_number(columnName) > 4001但收到ORA-01722无效数字错误。

I have then tried using Regexp_like unsuccessfully for this. 然后,我尝试为此使用Regexp_like失败。 I tried: 我试过了:

 1. 40[02,03,05,11,13]
 2. 40[0,1][^01]
 3. 40[0,1]([2,3,5,11])

Any help would be appreciated! 任何帮助,将不胜感激!

cheers 干杯

You could just use this condition: 您可以使用以下条件:

 col in ('4002','4003','4005','4011','4013')

If you really need to have to do it with regexp_like , then there are several ways to do it. 如果您确实需要使用regexp_like ,则可以通过多种方法进行。 Here are a few in order of decreasing readability and length: 以下是一些降低可读性和长度的顺序:

 regexp_like(col, '^(4002|4003|4005|4011|4013)$')
 regexp_like(col, '^40(02|03|05|11|13)$')
 regexp_like(col, '^40(0[235]|1[13])$')

Do notice that classes in regular expressions ( [ ... ] ) list individual characters to match, not sequences. 请注意,正则表达式( [ ... ] )中的类列出了要匹配的单个字符,而不是序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM