For example,
string is abc123CD
need to find out a method to read only numbers in the string
i.e.
select a_postgres_function(\'a
If you want to get all digit characters from the string (not just the first group), it is easier to remove all characters, which aren't a digit:
select regexp_replace('abc123CD45ef', '[^\d]+', '', 'g');
-- regexp_replace
-- --------------
-- '12345'
Try this:
select (regexp_matches('abc123CD', '\d+'))[1];
Since regexp_matches
returns array of text, you should access the first element by [1]
.
As per ntalbs's Answer
Wrap that query into a Function
create or replace function shownums(text) returns integer as
$$
select (regexp_matches($1,'\d+'))[1]::int;
$$
language sql;