I have several values in my name
column within the contacts
table similar to this one:
test 3100509 DEMO NPS
I want to return only the numeric piece of each value from name
.
I tried this:
select substring(name FROM '^[0-9]+|.*') from contacts
But that doesn't do it.
Any thoughts on how to strip all characters that are not numeric from the returned values?
The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.
Use the :: operator to convert strings containing numeric values to the DECIMAL data type. In our example, we converted the string ' 5800.79 ' to 5800.79 (a DECIMAL value). This operator is used to convert between different data types. It's very popular within PostgreSQL.
PostgreSQL supports the NUMERIC type for storing numbers with a very large number of digits. Generally NUMERIC type are used for the monetary or amounts storage where precision is required. Syntax: NUMERIC(precision, scale) Where, Precision: Total number of digits.
If you want to extract the numeric values with decimal point than use this
select NULLIF(regexp_replace(name, '[^0-9.]*','','g'), '')::numeric from contacts
select regexp_replace(name , '[^0-9]*', '', 'g') from contacts;
This should do it. It will work even if you have more than one numeric sequences in the name.
Example:
create table contacts(id int, name varchar(200));
insert into contacts(id, name) values(1, 'abc 123 cde 555 mmm 999');
select regexp_replace(name , '[^0-9]*', '', 'g') from contacts;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With