2

What would be the SQL to remove all numbers found in an otherwise string column using Sqlite (an Oracle example would be appreciated too)?

Example : I would like to remove all numbers from entries like this :

291 HELP,1456 CALL

Expected output:

HELP,CALL

edit: I have edited the question because it is not only from one entry that I want to remove numbers but many of them.

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
user3927897
  • 611
  • 3
  • 10
  • 20

3 Answers3

4

Either you do it in the language, you embedded sqlite, or you use this SQLite code, that removes all numbers:

UPDATE table SET column = replace(column, '0', '' );
UPDATE table SET column = replace(column, '1', '' );
UPDATE table SET column = replace(column, '2', '' );
UPDATE table SET column = replace(column, '3', '' );
UPDATE table SET column = replace(column, '4', '' );
UPDATE table SET column = replace(column, '5', '' );
UPDATE table SET column = replace(column, '6', '' );
UPDATE table SET column = replace(column, '7', '' );
UPDATE table SET column = replace(column, '8', '' );
UPDATE table SET column = replace(column, '9', '' );
Clemens Frahnow
  • 307
  • 1
  • 5
2

Using TRANSLATE and REPLACE

SQL> WITH DATA AS(
  2  SELECT '291 HELP' str FROM dual UNION ALL
  3  SELECT '1456 CALL' str FROM dual
  4  )
  5  SELECT REPLACE(translate(str, '0123456789', ' '), ' ', NULL) str
  6  FROM DATA
  7  /

STR
---------
HELP
CALL

SQL>

Using REGEXP_REPLACE

SQL> WITH DATA AS(
  2  SELECT '291 HELP' str FROM dual UNION ALL
  3  SELECT '1456 CALL' str FROM dual
  4  )
  5  SELECT trim(regexp_replace(str, '[0-9]+')) str
  6  FROM DATA
  7  /

STR
---------
HELP
CALL

SQL>

POSIX character class

SQL> WITH DATA AS(
  2  SELECT '291 HELP' str FROM dual UNION ALL
  3  SELECT '1456 CALL' str FROM dual
  4  )
  5  SELECT trim(regexp_replace(str, '^[[:digit:]]+')) str
  6  FROM DATA
  7  /

STR
---------
HELP
CALL

SQL>

Perl-extensions

SQL> WITH DATA AS(
  2  SELECT '291 HELP' str FROM dual UNION ALL
  3  SELECT '1456 CALL' str FROM dual
  4  )
  5  SELECT trim(regexp_replace(str, '\d+')) str
  6  FROM DATA
  7  /

STR
---------
HELP
CALL

SQL>
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
  • ERROR: function regexp_replace(text, unknown) does not exist – Luffydude Jun 16 '21 at 16:50
  • @Luffydude That's not an Oracle error, any Oracle error starts with `ORA-` error code followed by error message. Post your use case with the Oracle version and explain what you are actually trying to do. – Lalit Kumar B Jun 18 '21 at 20:21
1
select '''' || regexp_replace('123 help 321', '\d+') || '''' from dual;
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
ilj
  • 859
  • 8
  • 18