1

I am trying to get rows from the table Mails if the column [To] has the mail abc@mail.

The simple solution would be Select * from Mails where [To] = 'abc@mail'

But the thing is [To] column has data like 123@mail;abc@mail;aabc@mail etc separated by semicolons where To is multiple emails sent

I know I could do something like Select * from Mails where [To] like '%abc@mail%' but that won't solve the problem if the given mail is a substring of another mail. I thought of a split string solution

I have a split_string function like this,

CREATE FUNCTION [dbo].[split_string]
(
@string_value NVARCHAR(MAX),
@delimiter_character CHAR(1)
)
RETURNS @result_set TABLE(splited_data NVARCHAR(MAX)
)
BEGIN
DECLARE @start_position INT,
        @ending_position INT
SELECT @start_position = 1,
        @ending_position = CHARINDEX(@delimiter_character, @string_value)
WHILE @start_position < LEN(@string_value) + 1
        BEGIN
    IF @ending_position = 0 
       SET @ending_position = LEN(@string_value) + 1
    INSERT INTO @result_set (splited_data) 
    VALUES(SUBSTRING(@string_value, @start_position, @ending_position - @start_position))
    SET @start_position = @ending_position + 1
    SET @ending_position = CHARINDEX(@delimiter_character, @string_value, @start_position)
END
RETURN
END

which would return splitted string of a single data in a column and the function is working fine.

I tried executing the query

Select * 
from Mails 
where 'abc@mail' in (
   Select * 
   from dbo.split_string((SELECT [To] FROM Mails) , ';')
)

which is throwing the error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I need help proceeding from here. I am using Microsoft SQL Server 2014.

2
  • Side note, Multi-line table value functions are known to perform awfully. WHILE loops also perform very poorly. I strongly suggest that if you need to use a user defined function you make an inline table value function with a set based solution; it will significantly more performant.
    – Thom A
    May 12, 2022 at 11:55
  • If you do want a string splitter, I suggest looking into CLR functions, an XML splitter, or a tally splitter (like DelimitedSplit8K_LEAD/DelimitedSplitN4k_LEAD).
    – Thom A
    May 12, 2022 at 12:03

3 Answers 3

1

TL;DR; Here is the query that you want

SELECT *
FROM dbo.Mails AS m
WHERE EXISTS (
    SELECT *
    FROM dbo.split_string(m.[To], ';') s
    WHERE s.splited_data = 'abc@mail'
)

I recommend the splitting approach. Any character lookup will have to account the variability of the semi-colons, whereas splitting it out will handle the ambiguity of where the semi-colons are, and then you can do a direct equality check. If you wanted to take it a step further and look for additional [To] addresses you can just add an IN clause like this and SQL Server doesn't have to do much more work and you get the same results.

SELECT *
FROM dbo.Mails AS m
WHERE EXISTS (
    SELECT *
    FROM dbo.split_string(m.[To], ';') s
    WHERE s.splited_data IN ('abc@mail', 'def@mail')
)

My answer is fairly similar to @Kitta answer in that we split the data out, and @Kitta is correct about the IN clause, but while their answer will work it will require you grouping your data back together to get a singular answer. Using the EXISTS clause will bypass all of that for you and only give you the data from the original table. That being said, please mark @Kitta as the answer if their answer works just as well for you.

Here is the test setup that I used

DROP TABLE Mails
GO
CREATE TABLE Mails
([To] VARCHAR(3000))

INSERT INTO dbo.Mails
(
    [To]
)
VALUES
('123@mail;abc@mail;aabc@mail')
,('nottheone@mail.com')
,('nottheone@mail.com;Overhere@mail.com')
,('aabc@mail;ewrkljwe@mail')
,('ewrkljwe@mail')

GO
DROP FUNCTION [split_string]
GO
CREATE FUNCTION [dbo].[split_string]
(
@string_value NVARCHAR(MAX),
@delimiter_character CHAR(1)
)
RETURNS @result_set TABLE(splited_data NVARCHAR(MAX)
)
BEGIN
DECLARE @start_position INT,
        @ending_position INT
SELECT @start_position = 1,
        @ending_position = CHARINDEX(@delimiter_character, @string_value)
WHILE @start_position < LEN(@string_value) + 1
        BEGIN
    IF @ending_position = 0 
       SET @ending_position = LEN(@string_value) + 1
    INSERT INTO @result_set (splited_data) 
    VALUES(SUBSTRING(@string_value, @start_position, @ending_position - @start_position))
    SET @start_position = @ending_position + 1
    SET @ending_position = CHARINDEX(@delimiter_character, @string_value, @start_position)
END
RETURN
END
GO


SELECT *
FROM dbo.Mails AS m
WHERE EXISTS (
    SELECT *
    FROM dbo.split_string(m.[To], ';') s
    WHERE s.splited_data = 'abc@mail'
)
    

and it returns the correct row of '123@mail;abc@mail;aabc@mail'

1

You can't pass a multi-row subquery as an argument to the dbo.split_string function. Try to join your table function to the Mails table:

SELECT DISTINCT ms.*
    FROM Mails AS ms
    CROSS APPLY dbo.split_string(ms.[To], ';') AS s
    WHERE s.splited_data LIKE 'abc@mail'

If you can ubgrade your SQL Server up to 2016 (13.x), you can use built-in STRING_SPLIT table function instead of custom dbo.split_string.

Alternatively, you can achieve your goal with brute force and break down the comparison into simple terms such as follows:

SELECT * 
    FROM Mails 
    WHERE 
        [To] LIKE 'abc@mail'
        OR [To] LIKE '%;abc@mail;%'
        OR [To] LIKE 'abc@mail;%' 
        OR [To] LIKE '%;abc@mail'

It might not be the best way, but it's pretty simple and doesn't require a split function.

0

You can use CHARINDEX() function.

select * from Mails where CHARINDEX('abc@mail.com', To) > 0

The CHARINDEX function checks for a substring within a given string and returns a value greater than zero if it is found. Here your string to search (substring) would be 'abc@mail.com' and the main string from where to search would be the "To" column.

More information about CHARINDEX() function can be found at below link https://www.techonthenet.com/sql_server/functions/charindex.php#:~:text=SQL%20Server%3A%20CHARINDEX%20Function%201%20Description.%20In%20SQL,use%20the%20CHARINDEX%20function%20in%20SQL%20Server%20%28Transact-SQL%29.

1
  • I don't think this answer might solve this issue. As it is mentioned in the question, if the substring we want to search is itself a substring of another substring,(eg: searching for abc@mail will be True if the [To] column contains aabc@mail ), So using this might not be the way to go. May 13, 2022 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.