0

I have the following oracle query which runs fine

SELECT c.customercode, s.sales_id 
FROM customers c 
LEFT JOIN sales s ON c.customercode = s.sales_id
WHERE c.address IS NOT NULL

and it returns results looking something like this:

customercode        sales_id
12345678            456
34534534            678
23423423            X123

What I would like to do is exclude rows where the sales_id contains anything other than numeric characters. So the above results would not include the 3rd row.

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
Tom
  • 12,776
  • 48
  • 145
  • 240

2 Answers2

4

You can use regular expressions. In this case, regexp_like( sales_id, '^[[:digit:]]*$' )

SQL> with x as (select 12345678 code, '456' sales_id from dual union all
  2             select 34534534, '678' from dual union all
  3             select 23423423, 'X123' from dual)
  4  select *
  5    from x
  6   where regexp_like( sales_id, '^[[:digit:]]*$' );

      CODE SALE
---------- ----
  12345678 456
  34534534 678
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
1

TRANSLATE will work, too:

WITH q AS (
SELECT '12345678' customercode,'456' sales_id FROM dual
UNION ALL
SELECT '34534534','678' FROM dual
UNION ALL
SELECT '23423423','X123' FROM dual
)
SELECT *
  FROM q
 WHERE TRANSLATE(sales_id,'A1234567890','A') IS NULL;
DCookie
  • 42,630
  • 11
  • 83
  • 92