3

I have a table called "stuff" with a json column called "tags" that stores a list of tags, and a column called "id" which is the primary key for each row in the table. I am using a postgres database. For example, one row looks like this

   id | tags
   ---|------ 
   1  | ["tag1", "tag2", "tag3"]

I am trying to write a function that gets the rows which have a tag that matches the input tag variable called "s_tag". However, I want to convert all the tags to lowercase so that the tag matching is not case-sensitive.

I wrote the following test query:

     select id from stuff where '"tag1"' = any(select jsonb_array_elements(tags));

This query returned the ids of all the rows which had a tag called "tag1", but not "TAG1".

Then I read this post, so I tried

     select id from stuff where '"tag1"' like any(select jsonb_array_elements(tags));

But I got this error:

    ERROR: operator does not exist: unknown ~~* jsonb

So how can I convert the json list into a lowercase postgres array? Or is there some alternative way to search the list of tags in a case-insensitive way?

1 Answer 1

5

You can get the results as text instead of json:

select id 
from stuff 
where 'tag1' = any(select lower(jsonb_array_elements_text(tags)));
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.