0

I am trying to do something which I am not sure is possible or not.

So here it is, I have one data set in my PostgreSQL table:

select * from company;

 id |     name         | age | 
----+------------------+-----+-
  2 | Spine_[1-9]_Leaf |  12 |  

So basically I am storing a regular expression in name in the database. It means if we pass in the query any value like Spine_1_Leaf ... Spine_9_Leaf, it should return the record:

  2 | Spine_[1-9]_Leaf |  12 |

because name string matches name (regex from the db). Please do let me know how can we make it possible. I am trying things like:

select * from company where name ~ 'Spine_1_Leaf'

Which is not working (and it should not be also).

0

1 Answer 1

1

You've got the search string and term the wrong way round:

select * from company where 'Spine_1_Leaf' ~ name

is what you're after.

Sign up to request clarification or add additional context in comments.

3 Comments

The purpose I can see for the context of the question is to store some patterns in the database and then query individual values. So I would expect that the actual query would be select * from company where ? ~ name with ? filled with a user input...
Thanks beny it works I would like to know will it work for complex regex also or you will suggest some other approach
According to the Postgres docs, it'll work for any POSIX regexes: see here postgresql.org/docs/9.3/static/…

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.