Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am passing parameter serial no. as '18,8AB' comma separate string.

my table has serial no. data
8018A7400BC3
8018A79B6433
001DD55C6FAB
HDE09B356634
4650C7453C90
3275D8AB544D
4345D8AB544C

So, it should return result as
8018A7400BC3
8018A79B6433
3275D8AB544D
4345D8AB544C

any idea?

Thanks

What I have tried:

tried with Contain and like query.
Posted
Updated 8-Feb-18 6:30am
Comments
F-ES Sitecore 7-Feb-18 9:38am    
You'll need to explain why "18,8AB" returns those specific results from that data.
ZurdoDev 7-Feb-18 9:55am    
Google how to split a string in SQL.

You would have to split the string, and use the parts to construct an SQL command with two WHERE ... LIKE clauses
SQL
SELECT ... WHERE SerialNo LIKE '%18%' OR SerialNo LIKE '%8AB%'
You will then have to EXEC that SQL command.

It's a mess: SQL is not good at string handling, so this isn't a "nice" way to do it. This does something similar: Using comma separated value parameter strings in SQL IN clauses[^] and you could modify that, but ...

Honestly? I'd do it in my presentation language, rather than SQL.
 
Share this answer
 
Comments
Maciej Los 8-Feb-18 12:32pm    
5ed!
Another way to achieve that on SQL server server side is to use recursive queries named Common Table Expressions[^].
For further details, please see:
Using Common Table Expressions[^]
WITH common_table_expression (Transact-SQL) | Microsoft Docs[^]

I agree with OriginalGriff[^] (solution #1) that you should do that on presentation language (see example in c#).

Examples:

SQL
DECLARE @data TABLE (SerialNo VARCHAR(30));

INSERT INTO @data (SerialNo)
VALUES('8018A7400BC3'),
('8018A79B6433'),
('001DD55C6FAB'),
('HDE09B356634'),
('4650C7453C90'),
('3275D8AB544D'),
('4345D8AB544C');


DECLARE @find VARCHAR(30) = '18,8AB';

;WITH CTE AS 
(
	SELECT LEFT(@find, CHARINDEX(',', @find)-1) AS SerialPart, RIGHT(@find, LEN(@find) - CHARINDEX(',', @find)) AS Remainder
	WHERE CHARINDEX(',', @find)>0
	UNION ALL
	SELECT LEFT(Remainder, CHARINDEX(',', Remainder)-1) AS SerialPart, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM CTE
	WHERE CHARINDEX(',', Remainder)>0
	UNION ALL
	SELECT Remainder AS SerialPart, NULL AS Remainder
	FROM CTE
	WHERE CHARINDEX(',', Remainder)=0 
)
SELECT Orig.*
FROM @data AS Orig INNER JOIN CTE AS Parts ON Orig.SerialNo Like '%' + Parts.SerialPart + '%';


C#
//create sample data
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("SerialNo", typeof(string)));
dt.Rows.Add(new object[]{"8018A7400BC3"});
dt.Rows.Add(new object[]{"8018A79B6433"});
dt.Rows.Add(new object[]{"001DD55C6FAB"});
dt.Rows.Add(new object[]{"HDE09B356634"});
dt.Rows.Add(new object[]{"4650C7453C90"});
dt.Rows.Add(new object[]{"3275D8AB544D"});
dt.Rows.Add(new object[]{"4345D8AB544C"});

string find = @"18,8AB";

var result = dt.AsEnumerable()
	.Where(x => find.Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries).Any(y=> x.Field<string>("SerialNo").Contains(y)))
	.ToList();
	
foreach(DataRow dr in result)
{
	Console.WriteLine("{0}", dr.Field<string>("SerialNo"));
}



Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900