0

I have a column in database that has these values :

##1##,##7##,

##1##,##3##,##5##,

i need to check if ##3##, is exists or not .

i try to use like clause

declare @MyCSharpParameter

set @MyCSharpParameter = '##3##,##4##'

select * from myTable where col like '%@MyCSharpParameter%'

but,it's not work fine because myTable dose't have '##3##,##4##' it's have ##3##,##5##,

any help please to check it as separate numbers and find 3.

5
  • I removed the inconsistent database tags. Please tag with the database you a e really using. Then, fix your data model so you are not storing multiple values in a comma-delimited string. That is a SQL antipattern. Commented Jul 29, 2020 at 14:07
  • If you want to find "##3##' why don't you set your parameter like this set @MyCSharpParameter = '##3##' ? Commented Jul 29, 2020 at 14:10
  • @GordonLinoff i'm working on old project and its already built like this . i can't change the data model. Commented Jul 29, 2020 at 14:13
  • @DiogoMagro dear because these values it's come from C# in multi select, the user may select 3 and 4 i need to check if at least one value from the selected values is exists in mytable or not . Commented Jul 29, 2020 at 14:16
  • @QaisAlmomany Ok.. Did you check STRING_SPLIT? you can split you c# parameters by comma and then you have your parameters separated to check what you want Commented Jul 29, 2020 at 14:28

2 Answers 2

2

If you are using SQL Server 2016 or above, here is one way using string_split

declare @MyCSharpParameter nvarchar(50);
set @MyCSharpParameter = '##3##,##4##';

create table t (col varchar(100));

insert into t 
values('##1##,##7##'),('##1##,##3##,##5##');

select *, case when b.value is not null then 'yes' else 'no' end as found
from t a 
left join string_split(@MyCSharpParameter, ',') as b on a.col like '%'+b.value+'%';

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

i got this message in sql server 2017 " Invalid object name 'string_split'. "
2

You can use an exists subquery:

select t.*,
       (case when exists (select 1
                          from string_split(a.col, ',') s1 join
                               string_split(@MyCSharpParameter, ',') s2
                               on s1.value = s2.value
                         )
              then 'yes' else 'no'
        end)
from t;

This does not return duplicate rows if there are multiple matches.

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.