2

I had to select from the column all the values that contain numbers (NULL values aren't interested). How can I make a query to my table using something like this:

SELECT c1
FROM t1 
WHERE c1 ...;

I tried to find suitable regular expressions, which are popular in many imperative programming languages, but they didn't fit for PostgreSQL, unfortunately (or I used them incorrectly)

4 Answers 4

1

Possible solution:

SELECT c1 FROM t1 WHERE c1 ~ '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$';
Sign up to request clarification or add additional context in comments.

Comments

1

From Pattern Matching:


SELECT
    fld
FROM (
    VALUES ('1'),
        ('test1'),
        ('34'),
        ('dog'),
        ('3cat')) AS t (fld)
WHERE
    regexp_match(fld, '^[[:digit:]]{1,}$') IS NOT NULL;

fld 
-----
 1
 34


1 Comment

Thank you for your answer, but the third value (in example) is "34", this query returns "{3}", but I need "34". The above answer from M. Kuz. looks true.
1

Another possibility:

SELECT c1 FROM t1 WHERE c1 ~ '[0-9]';

From this table named STACKOVERFLOW:

id_stack name
1 Niklaus Wirth
2 Bjarne Stroustrup
3 Linus Torvalds
4 Donald Knuth
5 C3PO
6 R2D2
7 ZX Spectrum +3

The Query SELECT NAME FROM STACKOVERFLOW WHERE NAME ~ '[0-9]'; will return:

name
C3PO
R2D2
ZX Spectrum +3

Comments

0
SELECT * FROM <table> WHERE <column> ~ 'PRF:\d{11}-\d{4}'

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.