选择在VARCHAR列中仅具有数字的记录

时间:2019-04-18 13:29:14

标签: sql oracle

我正在尝试自我联接以查找属性长度不同且属性为全数字的记录组。

这是我到目前为止的查询:

SELECT * 
FROM MYTABLE t3
WHERE t3.GROUPID IN (SELECT t1.GROUPID
                     FROM MYTABLE t1, MYTABLE t2
                     WHERE t1.FILE_ID <> t2.FILE_ID
                       AND t1.GROUPID = t2.GROUPID
                       AND (length(t1.STR_FIELD) <> length(t2.STR_FIELD)))
  AND ORIG_GROUPID LIKE 'FRED_E%'
ORDER BY GROUPID ASC; 

我的表格当前如下所示:

-----------------------------
|GROUPID |FILE_ID| STR_FIELD|
| 1      | 12314 | 101      |
| 1      | 54246 | 1011     |
| 2      | 75375 | 202      |
| 2      | 24664 | 202M     |
-----------------------------

在上述情况下,我只希望返回GROUPID 1:

-----------------------------
|GROUPID |FILE_ID| STR_FIELD|
| 1      | 12314 | 101      |
| 1      | 54246 | 1011     |
-----------------------------

如何修改查询以允许这种情况发生?

2 个答案:

答案 0 :(得分:1)

这将起作用:

select * from Table1 where regexp_like(STR_FIELD,'^\d+$');

检查http://sqlfiddle.com/#!4/79c2f/5

select * from Table1 where regexp_like(STR_FIELD,'^\d*\d*$');

检查http://sqlfiddle.com/#!4/5a8e9b/1

答案 1 :(得分:0)

尝试一下

  select * FROM MYTABLE   t3
    WHERE t3.GROUPID IN (
      SELECT t1.GROUPID
      FROM MYTABLE   t1, MYTABLE  t2
      WHERE t1.FILE_ID <> t2.FILE_ID
      AND t1.GROUPID = t2.GROUPID
      AND (length(t1.STR_FIELD)<> length(t2.STR_FIELD)))
      and ORIG_GROUPID like 'FRED_E%'
      and (VALIDATE_CONVERSION(t1.STR_FIELD AS NUMBER) = 1)
      and (VALIDATE_CONVERSION(t2.STR_FIELD AS NUMBER) = 1)
      order by GROUPID asc;