-2

I am trying to pass a list of comma separated email ids a@b.com,s@b.com into sql in clause is my query right please give some suggestions

 SELECT * FROM service.cs_list_of_email_ids where Email in (
        SELECT regexp_substr(:a@b.com,s@b.com,'[^,]+', 1, level) items
        FROM dual
        CONNECT BY regexp_substr(:a@b.com,s@b.com, '[^,]+', 1, level) is not null
    );
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
shourya
  • 25
  • 1
  • 3

1 Answers1

0

Since you didn't mentioned if you are using Sql Server then you can create this function for splitting the comma separated string

CREATE FUNCTION Split
(
  @delimited nvarchar(max),
  @delimiter nvarchar(100)
) RETURNS @t TABLE
(
-- Id column can be commented out, not required for sql splitting string
  id int identity(1,1), -- I use this column for numbering splitted parts
  val nvarchar(max)
)
AS
BEGIN
  declare @xml xml
  set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

  insert into @t(val)
  select
    r.value('.','varchar(max)') as item
  from @xml.nodes('//root/r') as records(r)

  RETURN
END
GO

Then call it as below

SELECT * FROM service.cs_list_of_email_ids where Email in 
       (SELECT val FROM dbo.split(@str,','))
Reza
  • 18,865
  • 13
  • 88
  • 163