1

Similar to this question Find string match pattern . But this time I want to do this in PostgreSQL.

I have tried :

Select * from my_table where my_param SIMILAR TO "Delivered to (.*) at (.*)"

But it doesn't work.

2
  • Checkout LIKE Statement for this purposes usefull explanations here Commented Dec 25, 2015 at 8:47
  • Denys , sorry I have just edited the question , i have tried to use SIMILAR TO , not a plain one like before Commented Dec 25, 2015 at 8:54

1 Answer 1

2

You can use regexp_matches to extract part of text:

WITH cte AS
(
  SELECT col, regexp_matches(col, 'Delivered to (.*) at (.*)') AS r
  FROM tab
  -- WHERE col LIKE 'Delivered to % at %'
)
SELECT col, r[1] part1, r[2] part2
FROM cte;

SqlFiddleDemo

Output:

╔═══════════════════════════════════╦═══════════╦═════════╗
║               col                 ║  part1    ║  part2  ║
╠═══════════════════════════════════╬═══════════╬═════════╣
║ Delivered to Mr.Smith at Seattle  ║ Mr.Smith  ║ Seattle ║
╚═══════════════════════════════════╩═══════════╩═════════╝

If you only want to find rows without extracting use:

SELECT *
FROM tab
WHERE col LIKE 'Delivered to % at %'
Sign up to request clarification or add additional context in comments.

Comments

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.