繁体   English   中英

Oracle SQL查询以仅选择具有匹配表达式的列

[英]Oracle sql query to select columns only with matching expression

伙计们,我有包含以下数据的表

F$2051032568
F$2051032568
22342342343
3242432432432
5745745
45734554

当我编写一个oracle sql select查询时,我只想获取不以F $开头或不包含F $的数据。

22342342343
3242432432432
5745745
45734554

请告诉我,该怎么做。

其实这很棘手, 在sql查询的where条件中不使用$ ,我只需要获取那些与F$不匹配的数据。

尝试:

select col1 from tabl1 where col1 not like '%F$%';

我不确定是否可以理解您的修改:

select col1 from tabl1 where col1 not like ('%F' || chr(36) || '%');

如果您使用的脚本语言(例如Perl)将$%扩展为某种内容(可能是“”),则需要转义$。

例如,再次在Perl中,以下操作无效:

$select_statment = "select ... from ...where the Column not like '%F$%'";

但这将:

$select_statment = "select ... from ...where the Column not like '%F\$%'";
select col1 
from tabl1  
where instr(col1,'F$')=0

如果只有F $以外的数字,则可以执行以下操作:

select col1 
from tabl1  
where instr(col1,'F')=0

假设您只需要数字部分...您可以创建一个函数,该函数将使用TO_NUMBER函数来评估行值。 如果其无效文本,那么oracle将抛出ORA-01722: invalid number异常在函数中进行处理,然后可以在select查询中使用该函数。

使用regexp_like可以让您直接在where查询中包含正则表达式。

假设您只希望由数字组成的记录仅使用

select col1 from tab1
  where regexp_like(col1, '^[[:digit:]]*$');

如果要匹配F +而不是数字,可以使用

select col1 from tab1
  where not regexp_like(col1, 'F[^[:digit:]]');

暂无
暂无

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

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