0

I have a table contains customer details and i need to exclude certain emails from specific domains(identified from email string) from my table. How can i do that?

Tried with below but is not working any idea or suggestions ?

PostgreSQL Version 11

WITH CTE AS
(
select 'gmail.com' as name
union
select 'yahoo.com'
) 
,base as
(
    SELECT 'email ILIKE ''%'||name||'%''OR' as emails FROM CTE
)
,finals as
(
    SELECT string_agg(emails, '  ') as con FROM base
)
SELECT * FROm customers
WHERE  (SELECT con FROM finals )

Error i'm getting,

ERROR:  argument of WHERE must be type boolean, not type text
LINE 16: WHERE  (SELECT con FROM finals )
                ^
SQL state: 42804
Character: 242
2
  • Please provide input sample data and expected output. What exactly is not working? Commented Mar 2, 2021 at 8:34
  • @S-ManI have updated my question and included the error message,Any suggestions here please. Commented Mar 2, 2021 at 8:38

1 Answer 1

1

You cannot construct an SQL string and run it in a single SQL statement.

But I think this could be done much simpler:

WITH cte AS (
   SELECT 'gmail.com' AS name
   UNION ALL
   SELECT 'yahoo.com'
) 
SELECT * FROM customers
WHERE email ILIKE ANY (SELECT '%' || name || '%' FROM cte);
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.