0

I have following table in postgres 10.

col1        col2    col3                                            col4        
NCT00000102 Drug    nifedipine                                      nifed   
NCT00000102 Drug    nifedipine sulphate                             nifedipine  
NCT00000103 Drug    phospho nifedipine                              ni
NCT00000103 Drug    phospho nifedipine sulphate                     phospho nifedipine      
NCT00000105 Drug    fluticasone furoate (veramyst®) nasal spray     veramyst
NCT00000105 Drug    fluticasone furoate (veramyst®) nasal spray     vera
NCT00000106 Drug    veramyst                                        veramyst           

I am looking for a way to filter above table so that either only exact match or partial match but with the complete word/s included.

I am expecting below output.

col1        col2    col3                                            col4        
NCT00000102 Drug    nifedipine sulphate                             nifedipine  
NCT00000103 Drug    phospho nifedipine sulphate                     phospho nifedipine      
NCT00000105 Drug    fluticasone furoate (veramyst®) nasal spray     veramyst
NCT00000106 Drug    veramyst                                        veramyst           

I tried using below query but it is not giving me the partial matches properly.

where col3 '%' || col4 || '%'

Any suggestion here will be really useful.

1
  • Did you use LIKE? Commented May 25, 2020 at 8:43

1 Answer 1

1

Try using regex matching:

SELECT col1, col2, col3, col4
FROM yourTable
WHERE col3 ~* CONCAT('\y', col4, '\y');

This will return every record where the word or words in col4 appear as proper word(s) in col3. If you wanted to use LIKE, you could use:

WHERE col3 LIKE '%' || col4 || '%'

but keep in mind this might return false positives for substring matches.

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

4 Comments

WHERE col3 ~* '.*\y' || col4 || '\y.*'; gives below error: ERROR: argument of WHERE must be type boolean, not type text
.* in .*\y will make it match slower. Remove .* from the regex as ~* allows partial matches.
Does this where statement take only boolean. I am still getting this error.
@rshar Use CONCAT instead, or else you might need to do some casting.

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.