Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PostgreSQL: Partial match between string columns

I have the following code:

with my_table (id, col_1, col_2)
as (values 
(1, '$ (USD)', '$ (USD) million'),
(2, '$ (USD)', '$ (USD)'),
(3, 'USD', '$ (USD)'),
(4, 'EUR', '$ (USD)')
)

select *,
     case when col_1 = col_2 then 'TRUE'
     else 'FALSE'
     end as is_a_match
from my_table

the output:

id   col_1      col_2              is_a_match
1    $ (USD)    $ (USD) million    FALSE
2    $ (USD)    $ (USD)            TRUE
3    USD        $ (USD)            FALSE
4    EUR        $ (USD)            FALSE

how can I edit the query so I get id 1,2,3 as TRUE and id 4 as FALSE? I would like it to be generic, so if there are other currencies it can also look for a partial match.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use strpos() to check if the string in col_1 occurs anywhere in col_2:

select *,
       strpos(col_2, col_1) > 0 as is_a_match
from my_table
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading