Arzu
Arzu

Reputation: 46

How to get only alphabets from string in sql

I have a table "Temp". I want to extract only alphabets from the below table and want output as below: col

ABC123
456CDE
F789GH

OUTPUT

ABC
CDE
FGH

Upvotes: 0

Views: 11234

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522797

If using a database which supports REGEXP_REPLACE, then use this:

SELECT REGEXP_REPLACE(col, '\d', '', 'g') FROM yourTable;

The above is the call you would make for Postgres, for Oracle you don't need the g greedy flag as the replacement will be greedy by default.

If using a database without regex replacement support, e.g. MySQL or SQL Server, then we can still replace each digit individually:

SELECT
    REPLACE(REPLACE(REPLACE(col, '9', ''), '8', ''), '7', '')  -- ... replace 1-6 also
FROM yourTable;

Upvotes: 3

Serkan Arslan
Serkan Arslan

Reputation: 13403

If you are using oracle you can use REGEXP_REPLACE.

 SELECT REGEXP_REPLACE(col,'[0-9]','') FROM Temp

Upvotes: 1

Related Questions