简体   繁体   中英

Substring in Postgresql

I have a quick question. I'm studying some SQL exercises, and one of them, I need to remove the number from a street name.

For example:

  • 5026 S CRENSHAW BLVD - Should be CRENSHAW

  • 2635 WHITTIER BLVD - Should be WHITTIER

  • 308 WESTWOOD PLZ # 1390L - Should Be WESTWOOD

  • 1111 WILSHIRE BLVD - Should be WILSHIRE

Then, the answer to treat the address above was like this:

substring(facility_address FROM '[\d]+\s?\w?\s([\w]+)\s?')

I would like to understand how the substring works, what does it means de [\D] etc.

Could someone explain? Thank you very much indeed: :)

You should read up on regular expressions.

'[\d]+\s?\w?\s([\w]+)\s?' is a regular expression.

I'll try to break it down:

First of all, these are quantifiers:

+ means one or more

? means one or none

And now for the regular expression:

[\d]+ matches one or more digits. I think the square brackets are actually not necessary here.

\s? optionally matches a whitespace character (optionally meaning it may or may not be there)

\w? optionally matches a word character

\s matches a whitespace character, but this time it's not optional since there is no ? at the end

([\w]+) matches one or more word characters. Notice the parenthesis which denote a so called 'capture group`. Everything within the parenthesis is actually returned by the substring function.

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