SQL Select Statement LIKE IN - sql

I need to write a select statement which returns a list of users where a list of SenderSubIDs exist in a field called constraint_values:
I need a LIKE IN statement ideally:
SELECT SenderSubID FROM fix_user_subids WHERE SenderSubID IN ('**00390529MGERAN1**','**00912220PBALDIS**','**03994113LDAMBRO**','**04004308SLOMBAR**','**04935278CARELLI**','**4004308SLOMBARD**')
SELECT * FROM fix_dyno_rule_defs WHERE constraint_values LIKE '%**00390529MGERAN1**%'
Returns:
rule_def_id tag msg_type required constraint_values constraint_type data_type default_value validation_type trans_type attribute_tag trans_tag memo
99800 10000 D,F,G 0 ((50,4,1,00390529MGERAN1)or(50,4,1,00912220PBALDIS)or(50,4,1,03994113LDAMBRO)or(50,4,1,04004308SLOMBAR)or(50,4,1,04935278CARELLI)or(50,4,1,4004308SLOMBARD))and(21,4,1,3)#STROP1#addattr(EQD,EQST) 0 1 #TAG=6506# 12 1800 0 0 Set EQD=1 for Equity Desk
I wrote this:
SELECT * FROM fix_dyno_rule_defs WHERE constraint_values LIKE '%' + (SELECT MAX(SenderSubID) FROM fix_user_subids WHERE SenderSubID IN ('00390529MGERAN1','00912220PBALDIS','03994113LDAMBRO','04004308SLOMBAR','04935278CARELLI','4004308SLOMBARD')) +'%'
But need it without the MAX as there is a list...

You don't get your ideal. Use or:
SELECT SenderSubID
FROM fix_user_subid
WHERE SenderSubID like '%00390529MGERAN1%' OR
SenderSubID like '%00912220PBALDIS%' OR
SenderSubID like '%04004308SLOMBAR%' OR
SenderSubID like '%04935278CARELLI%' OR
SenderSubID like '%4004308SLOMBARD%'

/This should help you try this
declare a temp table and store all the IDs in it/
declare #tempSenderIDs Table(
T_TableID varchar(10)
)
insert into #tempSender
(
T_TableID
)
values
select
senderSubID
from
SenderSubIDs
/final result will come from here/
select
f.SenderSubIDs
from
fix_user_subid f
where
s.ConstraintValue like '%' + (select T_TableID from #tempSenderIDs) +'%'

If I'm reading your question right (and I might not be!) you're looking to get results from the table fix_dyno_rule_defs.
In which case, can't you just do a simple join?
ie:
SELECT DISTINCT fdrd.*
FROM fix_dyno_rule_defs fdrd
JOIN fix_user_subid fus
ON fdrd.constraint_values LIKE '%' + REPLACE(fus.SenderSubID, '*','') + '%'
WHERE fus.SenderSubID IN ('**00390529MGERAN1**','**00912220PBALDIS**',
'**03994113LDAMBRO**','**04004308SLOMBAR**',
'**04935278CARELLI**','**4004308SLOMBARD**')

Related

SQL Query Dynamically Create Multiple LIKE/OR Clause

I am trying to create the answer
SELECT *
FROM table
WHERE column LIKE 'Text%'
OR column LIKE 'Hello%'
OR column LIKE 'That%'
in below link:
Combining "LIKE" and "IN" for SQL Server
The problem is, in my example the values in the answer 'Text', 'Hello' and 'That' are not hard coded, they are populated from an application multi-select control and can be NULL value or a comma-separated string like this :
DECLARE #String_With_Commas nvarchar(255);
SET #String_With_Commas = N'Mercedes,BMW,Audi,Tesla,Land Rover';
I have tried below code, but it didn't work :
DECLARE #SearchString = CONCAT('''',REPLACE(#String_With_Commas, N',', N'%'' OR column LIKE '''));
And use it like :
WHERE column LIKE #SearchString + '%' + ''''
Assuming you are using a fully supported version of SQL Server, a couple ideas:
JOIN to STRING_SPLIT:
SELECT *
FROM dbo.YourTable YT
JOIN STRING_SPLIT(#YourVariable,',') SS ON YT.YourColumn LIKE SS.[value] + '%';
This will, however, return multiple rows if there can be multiple matches.
Use an EXISTS:
SELECT *
FROM dbo.YourTable YT
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT(#YourVariable,',') SS
WHERE YT.YourColumn LIKE SS.[value] + '%');
This won't return the same row twice, if there are multiple matches.
From the comments on this answer, the requirement that the parameter be NULLable was omitted in the question. I would therefore suggest you use the EXISTS solution:
SELECT *
FROM dbo.YourTable YT
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT(#YourVariable,',') SS
WHERE YT.YourColumn LIKE SS.[value] + '%')
OR #YourVariable IS NULL
OPTION (RECOMPILE);

How can I use contains to look for keyword including a space?

I want to use contains with search a word that has single space on left and right side. But it is not working..
SELECT *
FROM mytable
WHERE Contains(name, ' Hertz ');
Any help is appreciated..
You Can LTRIM RTRIM the column and can get the values.
create table #temp
(
name varchar(50)
)
insert into #temp values(' hertz')
insert into #temp values('hertz ')
insert into #temp values('hertzx')
insert into #temp values('hertz')
select * from #temp where LTRIM(RTRIM(name)) like '%hertz%'
If you want to search if it has space in start or end use below query.
select * from #temp where name like ' %'
select * from #temp where name like '% '
Try with this:
SELECT *
FROM mytable
WHERE name=' Hertz '
If Hertz is just an example, you can use:
SELECT *
FROM mytable
WHERE name like ' % '
which will return all the names with an space in the first and last position
Try as below.
SELECT *
FROM mytable
WHERE name LIKE '% Hertz %'
Learn more about like operator
You could try this
SELECT *
FROM mytable
WHERE name Like '% Hertz %'
To search with spaces included, you need to enclose your expression within double quotes, like :
SELECT *
FROM mytable
WHERE CONTAINS(name, '" Hertz "');
Of course this also can be written with LIKE (although it will be less efficient when dealing with large data sets) :
SELECT *
FROM mytable
WHERE name LIKE '% Hertz %');
If you use like operator, you no need to give spaces between that like below
select * from mytable where name like '%her%'
And also if you want the data of which having spaces, you can use like below
select * from mytable where name like '_%hertz%_'
If we use underscore, it will skip the particular position element and search the data.
Try this
Select * from mytable where replace(name,' ','')='Hertz'
You can also use ltrim & rtrim to remove spaces at run time to match the name values.
You can try this
SELECT a.name
FROM mytable a
WHERE a.name like ' %% '
OR
SELECT a.name
FROM mytable a
WHERE a.name like' % '
You can find the live demo Demo here

using on clause of a left outer join with the LIKE keyword using a dynamic list

I have a column in a table that can have a string CSV, I want to run a query against all those values with a LIKE that is apart of a ON clause to a outer join.
DECLARE #to VARCHAR(max) = (SELECT value FROM dbo.table WHERE id = 'key');
DECLARE #t table ( value varchar(max) )
INSERT INTO #t SELECT item FROM fn_csv_splitstring ( #to , ',' )
After I have gotten all the CSV values, I now want to have each value in my temp table used as an expression on the LIKE keyword similar to the below select
SELECT * FROM
dbo.table e
where e.value LIKE '%' + (SELECT value FROM [#t]) + '%'
The exact statement is used on a left outer join statement, the ON clause links two table row IDs
e.id = t.id and then there is an additional AND expression AND e.value LIKE 'col%' At this point I need to be able to have all rows in my temp table as a bunch of OR LIKE '%' or something that acts as LIKE '%' + (SELECT value FROM [#t]) + '%'
I have tried the IN keyword, but IN seems to only work with exact matches and not the wild card.
Thanks in advance.
Use EXISTS to check whether e.value is like ANY t.value (wildcarded)
SELECT *
FROM dbo.table e
WHERE EXISTS (
SELECT *
FROM #t t
WHERE e.value LIKE '%' + t.value + '%')

a sql query with multiple search word

i use following query to get all data which contains the some words (which is split by a function) in the name column of the inventoryLocalization table.
in the example i have split "red green blue" string.
as it should be, it returned all rows like OR operator.
SELECT distinct
inL.name
FROM dbo.[inventoryLocalization] inL
JOIN fnSplitString (N'red green blue',' ' ) words ON (inL.name LIKE '%'+ words.item +'%')
My question is, is it possible to get rows which has all words, as in the AND operator.
select inL.name from dbo.[inventoryLocalization] inL
where not exists
(select 1 from fnSplitString(N'red green blue',' ') words
where (inL.name NOT LIKE '%'+ words.item +'%'))
Try something like this:
DECLARE #SomeWords NVARCHAR(200), #Num INT
SET #SomeWords = 'red green blue'
SELECT #Num = COUNT(*)
FROM fnSplitString (#SomeWords,' ')
SELECT inL.name
FROM dbo.[inventoryLocalization] inL
JOIN fnSplitString (#SomeWords,' ' )words
ON (inL.name LIKE '%'+ words.item +'%')
GROUP BY inL.name
HAVING COUNT(*) = #Num

SQL Server statement Where myfield content giving sub-string

I'm looking for a SQL Server statement to retrieve records Where myfield content giving sub-string.
Another possibility is to use LIKE:
SELECT
MT.column_1,
....
FROM
My_Table MT
WHERE
some_column LIKE '%' + #search_string + '%'
You can use CharIndex
Select * From YourTable
Where
CharIndex('yoursubstring', myfield) > 0
Try PatIndex.
SELECT *
FROM Table
WHERE patindex('%string%', data ) > 0
select * from mytable where myfield like '%literalstring%'
or
select * from mytable where myfield like '%' + #stringvar + '%'
...not really clear on whether your substring is a literal or a variable