0

I have an animals table like this:

      Column      |           Type           | Collation | Nullable | Default 
------------------+--------------------------+-----------+----------+---------
 animal_code      | character varying(3)     |           | not null | 
 animal_type_code | character(2)             |           | not null | 
 description      | character varying(64)    |           | not null | 

Typical content might be:

 animal_code | animal_type_code |      description
-------------+------------------+------------------------------
 XAA         | T                | Not an animal, but a toaster
 1           | D                | This is a dog called Bob
 2           | C                | This cat is called Frank
 3           | C                | Wilf the cat has three legs
 4           | D                | Thunder is a dog

An existing stored procedure I'm working with receives a text string containing a comma-separated list of these animal_code values, like this:

store_pet_show_details(
  'London',                      -- p_venue      VARCHAR(64)
  '2019-12-03',                  -- p_date       TIMESTAMPTZ
  'XAA,91,22,23,74,15,64,47,12'  -- p_entrants   TEXT
);

I'm using unnest(string_to_array(code_csv, ',')) to extract the animal entry codes.

It's probably very simple, but I just want to see if any entrants have an animal_type_code of "T"

1 Answer 1

1

Please note that trim.

select animal_code, animal_type_code, description
 from animals
 inner join (select trim(e) entrant from unnest(string_to_array(p_entrants, ',')) e) t
 on animal_code = entrant
 where animal_type_code = 'T';
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.