Like Operator for checking multiple words - sql

I'm struggling for a like operator which works for below example
Words could be
MS004 -- GTER
MS006 -- ATLT
MS009 -- STRR
MS014 -- GTEE
MS015 -- ATLT
What would be the like operator in Sql Server for pulling data which will contain words like ms004 and ATLT or any other combination like above.
I tried using multiple like for example
where column like '%ms004 | atl%'
but it didn't work.
EDIT
Result should be combination of both words only.

Seems you are looking for this.
`where column like '%ms004%' or column like '%atl%'`
or this
`where column like '%ms004%atl%'

;WITH LikeCond1 as (
SELECT 'MS004' as L1 UNION
SELECT 'MS006' UNION
SELECT 'MS009' UNION
SELECT 'MS014' UNION
SELECT 'MS015')
, LikeCond2 as (
SELECT 'GTER' as L2 UNION
SELECT 'ATLT' UNION
SELECT 'STRR' UNION
SELECT 'GTEE' UNION
SELECT 'ATLT'
)
SELECT TableName.*
FROM LikeCond1
CROSS JOIN LikeCond2
INNER JOIN TableName ON TableName.Column like '%' + LikeCond1.L1 + '%'
AND TableName.Column like '%' + LikeCond2.L2 + '%'

Try like this
select .....from table where columnname like '%ms004%' or columnname like '%atl%'

Related

How to extract this specific data from a particular column in SQL Server?

I have column with below data:
Change
18 MCO-005329
A ECO-12239
0 ECO-25126
X1 ECO-05963
NA MCO-003778
C ECO-08399
MCO-003759
ECO-00643217
NULL
I want to extract the output like below:
MCO-005329
ECO-12239
ECO-25126
ECO-05963
MCO-003778
ECO-08399
MCO-003759
ECO-00643217
I have implemented the code like below:
select DISTINCT change,
case when change like 'MCO%' THEN change when change like 'ECO-%' THEN change
when change like '%MCO-%' then LTRIM(RTRIM(SUBSTRING(change,10,19) ))
when change like '%ECO-%' then LTRIM(RTRIM(SUBSTRING(change,10,19) ))
else '' end x from table
You can parse out the values from your requirements using SPLIT_STRING, outer apply, and a simple where clause without relying on hard coding any specific string length or position values, its dynamic.
SELECT D2.*
FROM
(
select '18 MCO-005329'
union select 'A ECO-12239'
union select '0 ECO-25126'
union select 'X1 ECO-05963'
union select 'NA MCO-003778'
union select 'C ECO-08399'
union select 'MCO-003759'
union select 'ECO-00643217'
union select NULL
) T(Change)
outer apply
(
select value
from
string_split(Change, ' ') d
) d2
where d2.value like '%-%' or d2.value is null
If you dont want nulls then smiply remove or d2.value is null
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver15
You could use CHARINDEX() and RIGHT() as
SELECT *, RIGHT(Change, CHARINDEX('-', REVERSE(Change)) + 3)
FROM
(
VALUES
('18 MCO-005329'),
('A ECO-12239'),
('0 ECO-25126'),
('X1 ECO-05963'),
('NA MCO-003778'),
('C ECO-08399'),
('MCO-003759'),
('ECO-00643217'), ('hhh kkk-k'),
(NULL)
) T(Change)

ORACLE SQL CSV Column comparison

I have a table with CSV values as column. I want use that column in where clause to compare subset of CSV is present or not. For example Table has values like
1| 'A,B,C,D,E'
Query:
select id from tab where csv_column contains 'A,C';
This query should return 1.
How to achieve this in SQL?
You can handle this using LIKE, making sure to search for the three types of pattern for each letter/substring which you intend to match:
SELECT id
FROM yourTable
WHERE (csv_column LIKE 'A,%' OR csv_column LIKE '%,A,%' OR csv_column LIKE '%,A')
AND
(csv_column LIKE 'C,%' OR csv_column LIKE '%,C,%' OR csv_column LIKE '%,C')
Note that match for the substring A means that either A,, ,A, or ,A appears in the CSV column.
We could also write a structurally similar query using INSTR() in place of LIKE, which might even give a peformance boost over using wildcards.
there's probably something funky you can do with regular expressions but in simple terms... if A and C will always be in that order
csv_column LIKE '%A%C%'
otherwise
(csv_column LIKE '%A%' AND csv_column LIKE '%C%' )
If you don't want to edit your search string, this could be a way:
select *
from yourTable
where csv like '%' || replace('A,C', ',', '%') || '%'
For example:
with yourTable(id, csv) as (
select 1, 'A,B,C,D,E' from dual union all
select 2, 'A,C,D,E' from dual union all
select 3, 'B,C,D,E' from dual
)
select *
from yourTable
where csv like '%' || replace('A,C', ',', '%') || '%'
gives:
ID CSV
---------- ---------
1 A,B,C,D,E
2 A,C,D,E
Consider that this will only work if the characters in the search string have the same order of the CSV column; for example:
with yourTable(id, csv) as (
select 1, 'C,A,B' from dual
)
select *
from yourTable
here csv like '%' || replace('A,C', ',', '%') || '%'
will give no results
Why not store the values as separate columns, and then use simple predicate filtering?

SQL Search rows that contain strings from 2nd table list

I have a master table that contains a list of strings to search for. it returns TRUE/FALSE if any string in the cell contains text from the master lookup table. Currently I use excel's
=SUMPRODUCT(--ISNUMBER(SEARCH(masterTable,[#searchString])))>0
is there a way to do something like this in SQL? LEFT JOIN or OUTER APPLY would be simple solutions if the strings were equal; but they need be contains..
SELECT *
FROM t
WHERE col1 contains(lookupString,lookupColumn)
--that 2nd table could be maintained and referenced from multiple queries
hop
bell
PRS
2017
My desired results would be a column that shows TRUE/FALSE if the row contains any string from the lookup table
SEARCH_STRING Contained_in_lookup_column
hopping TRUE
root FALSE
Job2017 TRUE
PRS_tool TRUE
hand FALSE
Sorry i dont have access to the DB now to confirm the syntax, but should be something like this:
SELECT t.name,
case when (select count(1) from data_table where data_col like '%' || t.name || '%' > 0) then 'TRUE' else 'FALSE' end
FROM t;
or
SELECT t.name,
case when exists(select null from data_table where data_col like '%' || t.name || '%') then 'TRUE' else 'FALSE' end
FROM t;
Sérgio
You can use a combination of % wildcards with LIKE and EXISTS.
Example (using Oracle syntax) - we have a v_data table containing the data and a v_queries table containing the query terms:
with v_data (pk, value) as (
select 1, 'The quick brown fox jumps over the lazy dog' from dual union all
select 2, 'Yabba dabba doo' from dual union all
select 3, 'forty-two' from dual
),
v_queries (text) as (
select 'quick' from dual union all
select 'forty' from dual
)
select * from v_data d
where exists (
select null
from v_queries q
where d.value like '%' || q.text || '%');

How select `Only String` and `Only Int`

I have some nvachar data in a my database.
aaaa , bb1b, c+cc , 1234 , dser , 5896
I need to two select for Only String and Only Int. This is mean :
Result of first select be : aaaa , dser
Result of second select be : 1234 , 5896
How?
You can use LIKE to perform this comparison. By using a double-negative, you can perform the tests with a single expression each:
declare #t table (val varchar(10) not null)
insert into #t(val)
select 'aaaa' union all
select 'bb1b' union all
select 'c+cc' union all
select '1234' union all
select 'dser' union all
select '5896'
select * from #t where val not like '%[^A-Z]%'
select * from #t where val not like '%[^0-9]%'
The first select says "give me all strings that don't contain a non-alphabetic character". Similarly, the second says "give me all strings that don't contain a non-digit"
You need to check for a regular expression.
In your example the query would look like this:
SELECT * FROM example WHERE foo LIKE '%[0-9]%' AND foo NOT LIKE '%[A-Z]%'
and
SELECT * FROM example WHERE foo NOT LIKE '%[0-9]%' AND foo LIKE '%[A-Z]%'
Maybe you have to do UPPERCASE(foo) for case insensitive checks. And add other characters like +, - and so on to the NOT LIKE expression.
See: http://msdn.microsoft.com/en-us/library/ms187489(SQL.90).aspx

SQL Server dynamic column list in contains function

I have a database table in SQL Server 2008 with 5 nvarchar(max) columns. We're using the CONTAINS function to look for text in these columns.
We can look in all five columns using this kind of query:
SELECT *
FROM SomeTable ST
WHERE CONTAINS( (ST.ColumnA, ST.ColumnB, ST.ColumnC, ST.ColumnD, ST.ColumnE) , '"Strawberry"')
Now we want to search for text for one or more of these columns dynamically. For example, only look in ColumnB and ColumnE. I tried using a CASE statement, but I couldn't.
The only solution I can think of is dynamic SQL, but I'd prefer to avoid this. (The full query is very complicated.) Is there a way to do this without using dynamic SQL?
The only way I can think of to avoid using dynamic SQL involves using a temp table and if statements and stored proc. Have a stored proc with parameters for each of the columns that include the text to search that columns for. Create a temp table or table variable to store interim results.
Have five different if statements one for each possible column. In each if insert to the temp table if the variable is not null. something like:
IF #ColA is not null
BEGIN
INSERT INTO #temp
SELECT * FROM SomeTable ST
WHERE CONTAINS( (ST.ColumnA) , #ColA)
END
At the end select from the temp table to show your result.
This only works well though if you don't have very many columns and it would be unlikely that more will be added. Frankly, the fact that you have multiple columns you need to do full text search for the same search string indicates to me that you may have a basic problem with the database design.
I just can think in a solution like this, but this doesn't use full text Search... let me know if it is useful for you.
SELECT *
FROM SomeTable ST
WHERE 1=1
AND ((#colA = '') OR ST.ColumnA LIKE #colA)
AND ((#colB = '') OR ST.ColumnB LIKE #colB)
AND ((#colC = '') OR ST.ColumnC LIKE #colC)
AND ((#colD = '') OR ST.ColumnD LIKE #colD)
AND ((#colE = '') OR ST.ColumnE LIKE #colE)
The search_on parameters would be bit (0, 1) so, when set on 1, the search on that column would be activated.
You could UNION the 5 individual cases, then PIVOT and concatenate. I'm not sure it's any better than dynamic SQL.
You would end up with something like:
SET #FindKey = '%B%E%' -- This is your search which field criteria
WITH RESULTS1 AS (
SELECT PK, 'A' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%A%' AND CONTAINS(ST.ColumnA, '"Strawberry"')
UNION
SELECT PK, 'B' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%B%' AND CONTAINS(ST.ColumnB, '"Strawberry"')
UNION
SELECT PK, 'C' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%C%' AND CONTAINS(ST.ColumnC, '"Strawberry"')
UNION
SELECT PK, 'D' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%D%' AND CONTAINS(ST.ColumnD, '"Strawberry"')
UNION
SELECT PK, 'E' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%E%' AND CONTAINS(ST.ColumnE, '"Strawberry"')
)
,RESULTS2 AS (
SELECT PK, ISNULL([A], '') + ISNULL([B], '') + ISNULL([C], '') + ISNULL([D], '') + ISNULL([E], '') AS FoundKey
FROM RESULTS PIVOT ( MIN(Col) FOR Col IN ([A], [B], [C], [D], [E]) ) AS pvt
)
SELECT *
FROM SomeTable
INNER JOIN RESULTS2
ON RESULTS2.PK = SomeTable.PK
WHERE RESULTS2.FoundKey LIKE #FindKey