0

I recently took over as db admin for an app I am unfamiliar with. I was poking around the development db instance and found a table with multiple values in a single column. I am not sure how to query on this column as what I have tried thus far has not worked.

I have tried various characters between the column name and what looks like a key value pair in the column value itself. I am trying to select based on the state :name value. None of the below have worked and I have not yet found the postgres doc that addresses this.

select * from district where state = 'California';

select * from district where state:name = 'California';

select * from district where state_name = 'California';

select * from district where state[name] = 'California';

select * from district where state[:name] = 'California';
select * from district where state[0] = 'California';

Below is a specific entry from the table districts

id         | 8
name       | 13
state      | --- + | :name: California + | :abbr: CA + |
created_at | 2011-12-08 04:31:15.104002
updated_at | 2011-12-08 04:31:15.104002
3
  • 1
    That is a custom format, that does not follow anything I have seen. You will have to parse that yourself using strpos() and substr(). There is no built-in function to deal with such a "structured string". What will obviously work for the example at hand is where state like '%California%' Commented Nov 17, 2020 at 16:54
  • @Abelisto output below select format('%L', state) from district where id = 8; ERROR: relation "district" does not exist LINE 1: select format('%L', state) from district where id = 8; Commented Nov 17, 2020 at 16:58
  • @a_horse_with_no_name Thanks, this query works. Moving on to how to parse in ruby rails. Commented Nov 17, 2020 at 16:59

1 Answer 1

1

Try a regular expression:

WHERE name  ~ '\mCalifornia\M'
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.