I have a table with column name that has random characters before and after the name ie:
Table A:
Name
-----------------
asd4345JONlkj345
.;lidDavidlksd$
and I have another table in same DB that has the names ie:
Table B:
Name
------
David
Jon
This goes on like this for 30k rows or I'd just hardcode something really quick. I want to search each string in Table A 'Name' column for each value from Table B, and if found return the name in a new column.
I have a feeling this will be a UDF, which is fine, I'm just unsure how to use patindex in this scenario, or if that is even the right approach.
You only need the LIKE operator for this:
select *
from TableA
inner join TableB on TableA.Name like '%' + TableB.Name + '%'
select B.Name, A.Name from tableA A inner join join tableB B
on rtrim(ltrim(B.Name)) like '%' + rtrim(ltrim(A.Name)) + '%'
Related
I have 2 SQL tables, one contains 2 columns, First Name and Last Name and the other has a column called NAME which has full names in it. I am trying to find if any of the values from the Last Name column in table1 can be found in the NAME column of table2. So far I have tried the following query without much success:
SELECT * FROM table1
CROSS APPLY
(SELECT * FROM table2 WHERE ('%'+table1.[Last Name]+'%' LIKE table2.NAME)) table2
I am not sure, but I think this works:
SELECT *
FROM table1
WHERE ISNULL((SELECT TOP(1) 1 FROM tabl2 WHERE Name LIKE '%' + table1.LastName + '%'), 0)= 1
This query's accuracy can suffer in case of similar first and last names. You can add white spaces for better accuracy: LIKE '% table1.LastName %'.
Azure SQL Server 2017:
We have a table called dbo.keywords with one column, called keywords. This column consists of ~10,000 varchar(50) entries.
We have another table called dbo.articles. Both tables are in the same database.
The query we are trying to create would be such:
SELECT * FROM dbo.articles
WHERE TextValue LIKE
(**any of the 10,000 values of the keywords column in the dbo.keywords table**).
The part in the parentheses above is the part that I am unclear on accomplishing. If not for the LIKE part, I suppose I could use a SELECT subquery, but the LIKE piece is what's throwing me.
I suspect that this will be the more "perfomant" option (I use quotes, as using LIKE with a leading wildcard will make the query non-SARGable):
SELECT *
FROM dbo.articles a
WHERE EXISTS (SELECT 1
FROM dbo.keywords k
WHERE a.TextValue LIKE '%' + k.keyword + '%');
This will avoid duplicate rows, and a costly DISTINCT; as I suspect that TextValue could have some lengthy values.
You can achieve it by using JOIN with LIKE.
Could you please try the query below:
SELECT *
FROM dbo.articles [AR]
INNER JOIN dbo.keywords [KW] ON [AR].TextValue LIKE '%' + [KW].keywords + '%';
You'll want to join the tables, and use the LIKE clause in the join condition:
SELECT DISTINCT a.* FROM dbo.articles a
JOIN dbo.keywords k ON a.TextValue LIKE '%' + k.keywords + '%';
I need to find a way to join two tables based using the columns' names with wild cards. This is the scenario I have:
CREATE TABLE #table1
(
AuthCode varchar(10)
)
INSERT INTO #table1
VALUES ('52201')
SELECT * FROM #table1
JOIN bilact
ON #table1.AuthCode = bilact.byAuthCode
In bilact table, the auth code is 005221 (the field is a varchar) and when I run the above query, it does not return any result.
The situation I am running into is so complicated and i do not want to explain why I must do it this way cause it is the only way that will work. Is there a way I can add the wild card with the column name in a join? Thank you.
Assuming you want one column to be the substring of another:
SELECT * FROM #table1
JOIN bilact
ON str(bilact.byAuthCode) LIKE '%' + #table1.AuthCode + '%'
or, if you need to match a substring in either table
SELECT DISTINCT * FROM #table1
JOIN bilact
ON str(bilact.byAuthCode) LIKE '%' + #table1.AuthCode + '%'
OR #table1.byAuthCode LIKE '%' + str(bilact.AuthCode) + '%'
You can remove the str()'s if both columns are varchars or some form of string field.
Otherwise, you need to decide what the longest common substring the 2 columns need in common, and in the join, iterate over every possible substring of one of the columns and do a like as above with the other column against it.
I'm trying to work out a SQL Select in MS SQL 2005, to do the following:
TABLE_A contains a list of keywords... asparagus, beetroot, beans, egg plant etc (x200).
TABLE_B contains a record with some long free text (approx 4000 chars)...
I know what record within TABLE_B I am selecting (byID).
However I need to get a shortlist of records from TABLE_A that are contained WITHIN the text of the record in TABLE_B.
I'm wondering if SQLs CONTAINS function is uselful... but maybe not.
This needs to be a super quick query.
Cheers
It will never be super quick because of the LIKE and wildcard at each end. You can not index it and there are no whizzy tricks. However, because you have already filtered TableB then it should be acceptable. If you had a million rows in tableB, you could go for coffee while it ran
SELECT
A.KeyWordColumn
FROM
TableA A
JOIN
TableB B ON B.BigTextColumn LIKE '%' + A.KeyWordColumn+ '%'
WHERE
B.ByID = #ID --or constant etc
CONTAINS can be used if you have full text indexing: but not for a normal SQL query
I would try this
select keyword from table_a, table_b
where table_b.text like '%' + keyword + '%'
and table_b.Id = '111'
I had a requirement to create a query in SQL Server where the search condition would include/exclude a table based on user input.
Say I have two tables, TABLE_A and TABLE_B with columns KEYCOLUMN_A and COLUMN_A in TABLE_A and columns FKCOLUMN_B and COLUMN_B in TABLE_B.
And a query like:
SELECT TABLE_A.* FROM TABLE_A, TABLE_B WHERE TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
AND TABLE_A.COLUMN_A LIKE '%SEARCH%' AND TABLE_B.COLUMN_B LIKE '%SEARCH2%'
Now if the user does not input SEARCH2, I don't need to search TABLE_B. But this would mean an IF ELSE clause. And as the number of "optional" tables in the query increases, the permutations and combinations would also increase and there will be many IF and ELSE statements.
Instead I decided to keep the statement as it is. So if SEARCH2 is empty, the query will effectively become:
SELECT * FROM TABLE_A, TABLE_B WHERE TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
AND TABLE_A.COLUMN_A LIKE '%SEARCH%' AND TABLE_B.COLUMN_B LIKE '% %'
Can the SQL optimizer recognize that LIKE %% is as good as removing the condition itself?
Wrap an OR around your "B" table, such as:
AND (len(searchString)=0 OR table_b.column_b LIKE "%searchString%" )
This way, if no value for the string, its length would be zero, and the first part of the OR would be evaluated, always come back as true and return that portion of the equation as valid and ignore the other half using the LIKE clause.
You could apply the same for as many linked tables as you need.
First thing, you have a space in your example:
AND TABLE_B.COLUMN_B LIKE '% %'
That will never be optimized as it is indeed a significant condition.
Now, I think that if it is optimized away depends on the database engine and how smart it is.
For example, SQL Server 2005 does offer the same execution plan for the two types of queries, while MySQL 5.0.38 does not.
LIKE is used with the WHERE clause to search, update, and delete a record using wild cards.
Example:
To search all records whose employee name is starred from a character, 'a':
select * from Employee where Name like 'a%'
To update all records with name amit whose employee name is starting with a character, 'a':
update Employee set Name='amit' where Name like 'a%'
To delete all records whose employee name is starting with a character, 'a':
delete from Employee where Name like 'a%'
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column
LIKE '%[p-s]' -- "It search data from table parameter where sentence ending with p,q,r,s word."
LIKE '[0-9]' --It use for search only numeric value
LIKE '%table%' -- it use for search parameter from table where use "table" keyword'.
LIKE %[^p-r] -- it set the condition where Not Ending With a Range of Characters
Example:
SELECT T1.BrandName,T1.BrandID,T2.CategoryName,T2.Color FROM TABLE1 T1
LEFT JOIN TABLE2 T2 on T1.ID = T2.BrandID
WHERE T1.BrandName LIKE '%Samsung%'
Example:
SELECT T1.BrandName,T1.BrandID,T2.CategoryName,T2.Color FROM TABLE1 T1
LEFT JOIN TABLE2 T2 on T1.ID = T2.BrandID
WHERE T1.BrandName LIKE '%[a-j]'
In MySQL you can also use ILIKE, and then it's case insensitive.
You can rewrite you query like this:
SELECT TABLE_A.* FROM TABLE_A, TABLE_B WHERE TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
AND (#paramA='' or TABLE_A.COLUMN_A LIKE '%' + #paramA + '%')
AND (#paramB='' or TABLE_B.COLUMN_B LIKE '%' + #paramB + '%')
This way, if paramA or paramB is '', then the other column that is queried inside same parentheses will not be queried.
Use UNION and proper JOINs.
The %foo% search term is bad enough (can't use index) without adding OR and LEN to the mix too.
SELECT
TABLE_A.*
FROM
TABLE_A
JOIN
TABLE_B On TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
WHERE
TABLE_A.COLUMN_A LIKE '%SEARCH%' AND TABLE_B.COLUMN_B LIKE '%SEARCH2%'
UNION
SELECT
TABLE_A.*
FROM
TABLE_A
WHERE
TABLE_A.COLUMN_A LIKE '%SEARCH%'