PostgreSQL substring gets a line between brackets

I have a line, say:

Product Description [White]

I want to extract something inside the brackets (in this case White) from this line using the PostgreSQL substring function. I can get this to work with regexp_matches, but it returns an array that I don't want if I have no other choice.

I tried:

  • substring('string' from '[(.)]') →> NULL
  • substring('string' from '\[(.)\]') →> NULL
  • substring('string' from '\\[(.)\\]') →> NULL

But it works:

  • substring('string' from 'W(.)i]') →> h

What am I doing wrong?

+4
source share
2 answers

(.) matches only one character, but you want to combine several characters with it.

So you need (.+)

substring('Product Description [White]' from '\[(.+)\]')
+9
source

, - , , ,

UPDATE master.pay_scale  SET grade_pay = Case LIKE '% (%)%' ( ( '((. +))')) Else description End:: numeric

-1

Source: https://habr.com/ru/post/1620581/


All Articles