简体   繁体   中英

Postgres SQL string matching in where

What's the simplest way to write a SQL query (postgres) for the following: I have an adress table, that has a streetnumber column. That can contain digits and letters. And I if an argument is say '3' I want to return rows with values starting with three followed by something that is not a digit. So for example rows with streetnumber values 3, 3A, 3 A, but not 33.

You can use matching using regular expressions .

The query may look like the following:

SELECT * FROM table WHERE streetnumber ~ '3[^0-9]*'

The regular expression pattern 3[^0-9]* matches " digit 3 followed by zero or more characters which are not a digit ".

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