Skip to content
Advertisement

Perform a particular ILIKE query on Postgresql

I have a problem with a query in Postgres.

I have a column with values such as:

id@@@Name1@@@other text@@@other_text@@@other_text@@@CODE@@@other_text###    
id@@@Name2@@@other text@@@other_text@@@other_text@@@CODE@@@other_text###   
id@@@Name3@@@other text@@@other_text@@@other_text@@@CODE@@@other_text###

Each row can have different string suchs as the above. The @@@ separates each part of the string-block.

The ### separates each string-block.

@@@ and ### are so a “references”.

With my query I have to find, in example:

  • The row which block has Name1 and CODE='X-X'

The query:

doesn’t work. It doesn’t work because the % makes the query take ALL possible parts of text between.

So potentially, it can take (mistakenly) also the table row with Name1 (ok), but where the code X-X is, instead, of the string-block of the Name10 as the example below:

1@@@Name1@@@othertext@@@othertext@@@WRONGCODE@@@othertext### … … 10@@@Name10@@@othertext@@@othertext@@@X-X@@@othertext###


SQL provides only 2 wild cards:

  • % = take all ,
  • _ = jolly only 1 char

But I don’t know how much characters are in the othertext part of the block…

Is there any possible way to perform this with regex? Or make the % work only between 2 fixed parts?

Advertisement

Answer

You could use split_part()

split_part() splits the input string based on the delimiter (second parameter) into individual elements. The third parameter identifies the number of the elements (starting at 1, which would be the id in your case)


As this gets quite cumbersome if you need to use that as part of other statements, I would create a view, that turns the blocks into columns that are arrays:

This is a bit faster than calling split_part multiple times as the parsing/splitting is only done twice per row, not twice for each item.

Then you can do something like:

Or if you need to check multiple items:


Another option is to create one array with all elements from the same position:

Then you can do queries like:


And there is also the option to convert the whole delimited string into a JSON value so that you can access the elements by key, rater than position.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement