简体   繁体   中英

how can I delete alphanumeric rows from a column in postgresql?

Suppose my column name is "psdi", i want to print only numeric rows

psdi
100
10k
103f
456
9o2u
125
931

Required O/P:

psdi
100
456
125

Here are two simple methods. One looks for letters:

select t.*
from t
where not psdi ~ '[a-zA-Z]'

or for just digits:

select t.*
from t
where not psdi ~ '^[0-9]+$'

Use pattern matching in where clause

'^-?([0-9]+\\.?[0-9]*|\\.[0-9]+)$' - This selects all type of values(positive, negative and floating values)

select *
from (
    values ('100T')
          ,('456.5')
          ,('-65')
          ,('9879t')
          ,('454')
    ) t(a)
where a ~ '^-?([0-9]+\.?[0-9]*|\.[0-9]+)$'

demo


Your query will be

select psdi
from your_table
where psdi ~ '^-?([0-9]+\.?[0-9]*|\.[0-9]+)$'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM