Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres query to check a string is a number

Tags:

postgresql

Can anyone tell me the query to check whether a string is a number(double precision). It should return true if the string is number. else it should return false.

consider :

       s1 character varying;        s2 character varying;         s1 ='12.41212' => should return true        s2 = 'Service' => should return false 
like image 961
user2990782 Avatar asked Nov 14 '13 10:11

user2990782


People also ask

How do you check if an element in a string is a number?

A simple approach to check if a Python string contains a number is to verify every character in the string using the string isdigit() method. Once that's done we get a list of booleans and if any of its elements is True that means the string contains at least one number.

Is Numeric in Postgres?

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.

How do I search for a string in PostgreSQL?

Like operator is used to find or retrieve data to specified matching patterns. % (wildcard) is used to get all characters. % it is used to retrieve one or more characters from the database. _ (Underscore) is used to match any number of single characters.


1 Answers

I think the easiest way would be a regular expression match:

select '12.41212' ~ '^[0-9\.]+$' => true  select 'Service' ~ '^[0-9\.]+$' => false 
like image 127
a_horse_with_no_name Avatar answered Sep 21 '22 14:09

a_horse_with_no_name