Query SQL with like operator from two tables - sql

How can I do a SQL query with the like operator from two different tables?
I need something like:
select * from table1 where name like %table2.name
It's not a common field but a substring of a field on another table.

Edit
(original answer is further down)
Your comment (and subsequent edit) completely changes the question.
To do that, you can use LIKE as part of the ON clause in a join:
CREATE TABLE a (foo varchar(254))
GO
CREATE TABLE b (id int, bar varchar(254))
GO
INSERT INTO a (foo) VALUES ('one')
INSERT INTO a (foo) VALUES ('tone')
INSERT INTO a (foo) VALUES ('phone')
INSERT INTO a (foo) VALUES ('two')
INSERT INTO a (foo) VALUES ('three')
INSERT INTO b (id, bar) VALUES (2, 'ne')
INSERT INTO b (id, bar) VALUES (3, 't')
SELECT a.foo
FROM a
INNER JOIN b ON a.foo LIKE '%' + b.bar
WHERE b.id = 2
(That's the SQL Server version; for MySQL, add in the various semicolons, remove the GOs, and use ...LIKE concat('%', b.bar) instead.)
That uses id = 2 to find bar = "ne" in table b, then prepends the % operator and uses it to filter results from a. Results are:
one
tone
phone
You won't have to do the concat if you can store the operator in b.bar.
Separately, I was surprised to find that this works (on SQL Server) as well:
SELECT foo
FROM a
WHERE foo LIKE (
SELECT TOP 1 '%' + bar
FROM b
WHERE id = 2
)
...but the version using JOIN is probably more flexible.
That should get you going.
Original answer
(Arguably no longer relevant)
It's hard to tell what you're asking, but here's an example of using LIKE to limit the results from a JOIN:
SELECT a.foo, b.bar
FROM someTable a
INNER JOIN someOtherTable b
ON a.someField = b.someField
WHERE a.foo LIKE 'SOMETHING%'
AND b.bar LIKE '%SOMETHING ELSE'
That will give you foo from someTable and bar from someOtherTable where the rows are related by someField and foo starts with "SOMETHING" and bar ends with "SOMETHING ELSE".

Not particularly sure about the precise syntax, but here's an idea:
select ... from (
select ID, Foo as F from FooTable
union all
select ID, Bar as F from BarTable) R
where R.F like '%text%'

Based on #TJCrowder answer
Test Tables
create table #A (
id varchar(10)
)
create table #b(
number varchar(5)
)
insert into #A values('123')
insert into #A values('456')
insert into #A values('789')
insert into #A values('0123')
insert into #A values('4567')
select * from #A
insert into #B values('12')
insert into #b values('45')
insert into #b values('987')
insert into #b values('012')
insert into #b values('666')
Actual query
select * from #a, #b
where #a.id like '%' + #b.number + '%'
Modify the above query as you need such as
select #A.* from #A,#B ...
or
select * from #a, #b
where #a.id like #b.number + '%' -- one side match

Related

SQL Server 2012 How to determine if all the values in table A are in table B

I have two tables (A & B) that share a common value (Color), both tables can have any number of rows. I am trying to find a way to determine if all the distinct 'Colors' in table A exist in table B:
I have tried using EXCEPT, which almost works, unfortunately it returns false when table B has more Colors than table A, which is irrelevant all i care about is if every distinct Color from table A is in table B. I have been fiddling with both EXISTS and IN but can't see to get the correct results
declare #TableA table (Color varchar(10))
declare #TableB table (Color varchar(10))
insert into #TableA(Color) values ('red')
insert into #TableA(Color) values ('blue')
insert into #TableA(Color) values ('green')
--insert into #TableA(Color) values ('orange')
insert into #TableB(Color) values ('red')
insert into #TableB(Color) values ('blue')
insert into #TableB(Color) values ('green')
insert into #TableB(Color) values ('yellow')
insert into #TableB(Color) values ('purple')
IF NOT EXISTS (
SELECT Color FROM #TableA
EXCEPT
SELECT Color FROM #TableB
)
SELECT 'true'
ELSE SELECT 'false'
I would like the above code to yield 'true'.
IF table A Colors > table B Colors THEN false
IF table A Colors <= table B Colors THEN true.
There are many ways to accomplish this. You could use a left join for this pretty easily.
if exists
(
SELECT a.Color
FROM #TableA a
left join #TableB b on b.Color = a.Color
where b.Color is null
)
select 'Some Colors in A are not in B'
else
select 'ALL Colors in A exist in B'
you could also just use your existing query and add DISTINCT:
IF NOT EXISTS (
SELECT DISTINCT Color FROM #TableA
EXCEPT
SELECT DISTINCT Color FROM #TableB
)
SELECT 'true'
ELSE SELECT 'false'

Copy records from one table to another without duplicates

IF object_id('tempdb..#A') IS NOT NULL DROP TABLE #A
IF object_id('tempdb..#B') IS NOT NULL DROP TABLE #B
CREATE TABLE #A (fname varchar(20), lname varchar(20))
CREATE TABLE #B (fname varchar(20), lname varchar(20))
INSERT INTO #A
SELECT 'Kevin', 'XP'
UNION ALL
SELECT 'Tammy', 'Win7'
UNION ALL
SELECT 'Wes', 'XP'
UNION ALL
SELECT 'Susan', 'Win7'
UNION ALL
SELECT 'Kevin', 'Win7'
SELECT * FROM #A
INSERT INTO #B
SELECT a.fname, a.lname FROM #A a
WHERE a.fname NOT IN (SELECT fname from #B)
SELECT * FROM #B
DELETE FROM #B
INSERT INTO #B
SELECT a.fname, a.lname FROM #A a
LEFT OUTER JOIN #B b ON a.fname = b.fname
WHERE a.fname NOT IN (SELECT fname from #B)
SELECT * FROM #B
Both of these examples copy all 5 records to the new table.
I only want to see one unique fname so only one Kevin should show up.
Why don't these work, or is there a better way to do it?
It seems like such a simple thing.
This would create rows with unique fname and take Win7 if both Win7 and XP existed.
INSERT INTO #B
SELECT a.fname, MIN(a.lname)
FROM #A a
GROUP BY a.fname
As per comments, given that W comes before X then you should be able to do
INSERT INTO #B
SELECT fname, lname
FROM (
SELECT fname, lname,
ROW_NUMBER() OVER(PARTITION BY fname ORDER BY lname) r
FROM #A
) t
WHERE r=1
demo
Answering your question, why don't your queries work?
INSERT INTO #B
SELECT a.fname, a.lname FROM #A a
WHERE a.fname NOT IN (SELECT fname from #B)
This operation is evaluated in two different operations. In the first, the SELECT part of the query is executed. It returns a table. At such point #B is empty, hence, every tuple in #A will be part of this result. Then, once this result is computed, this result is inserted into #B. #B will end being a copy of #A.
The DBMS does not insert one tuple, and then re-evaluate the query for the next tuple of #A, as your question seems to imply. Insertions are always done AFTER the query has been completely evaluated.
if your goal is to insert into #B the tuples in #A without duplicates, there are many ways to do that. One of them is:
INSERT INTO #B SELECT distinct * from #A;
--dmg
Just use DISTINCT over the select query :
INSERT INTO TARGET_TABLE
SELECT DISTINCT * FROM
(
-- some big query
) x

sql like clause multiple values

I have a table with multiple words, from 1 to n.
declare #words table
(
word varchar(100) not null
)
insert into #words (word) values ('word1')
insert into #words (word) values ('word2')
insert into #words (word) values ('word3')
declare #tablea table
(
column1 varchar(100) not null
)
insert into #tablea (column1) values ('aword1a aword2a aword3a')
insert into #tablea (column1) values ('word2a')
insert into #tablea (column1) values ('word3a')
Im having trouble to write a query to select from a table where a column is like these words, and I need the AND operator. If the table contains word1, word2, word3, the like clause must match the three words, it means, I want to return the first row in tablea.
select *
from tablea
where
column1 like ?
Updated:
select t.column1
from #tablea t
inner join #words w on charindex(w.word, t.column1) > 0
group by t.column1
having count(distinct w.word) = (select count(*) from #words)
Since any one column needs to contain all the values in the #words table, I would use a not exists, and try to find a value in #words that isn't contained in the column1 field.
select
*
from
#tablea a
where not exists (
select 1
from #words w
where a.column1 not like '%' + w.word + '%'
)
This will do it, but I'm not sure how extensible it is:
SELECT column1
FROM #tablea t
JOIN #words w ON t.column1 LIKE '%'+w.word+'%'
GROUP BY column1
HAVING COUNT(*) = (SELECT COUNT(*) FROM #words)
In the long run, you may be better off implementing Full Text Search.

Convert Rows Into Columns in SQL

Table A
ID COLA
-----------------------
A value1
B value1
C value1
Table B
ID DETAIL_ID COL_X COL_Y
A 0 foo foo
A 1 bar bar
B 0 foo foo
My expected out is something like
ID COLA COL_X_0 COL_X_1 COL_Y_0 COL_Y_1
A value1 foo bar foo bar
B value1 foo NULL foo NULL
C value1 NULL NULL NULL NULL
It means the rows of table B will be column values based on DETAIL_ID column.
I tried to write queries for this , but can't succeed due to following.
Number of DetailID values will NOT be fixed-length.It means I can't hard-coded the name of the columns.
This will give the exact output you described and you can add more columns if needed
DECLARE #a table (id char, cola varchar(10))
DECLARE #b table (id char, detail_id int, colx char(3), coly char(3))
INSERT #a values('A', 'value1'),('B', 'value2'),('C','value3')
INSERT #b values('A', 0, 'foo', 'foo'),('A', 1, 'bar', 'bar'),
('B',0, 'foo','foo')--,('A', 2, 'bar', 'bar') -- add this for extra columns
CREATE TABLE ##t(id char, detail_id tinyint, colvalue char(3), col varchar(8), cola varchar(10))
DECLARE #columns varchar(max)=''
DECLARE #sqlstring varchar(1000)
;WITH a as (
SELECT a.id, a.cola, b.detail_id, colx, coly,
'col_x_' + cast(detail_id as varchar) col_a,
'col_y_' + cast(detail_id as varchar) col_b
FROM #a a LEFT JOIN #b b on a.id = b.id
)
INSERT ##t
SELECT id, detail_id, colx, col_a, cola FROM a
UNION
SELECT id, detail_id, coly, col_b, cola FROM a
ORDER BY 4,2
SELECT #columns = coalesce(#columns, '') +',[' + col + ']'
FROM (
SELECT DISTINCT col, detail_id FROM ##t where not col is null
) a
SET #columns = stuff(#columns, 1,1,'')
SET #sqlstring =
'SELECT * FROM (
SELECT id, cola, col, colvalue FROM ##t
) b
PIVOT(max(colvalue) FOR col
in(
'+#columns+'))AS p order by 1'
EXEC(#sqlstring)
DROP TABLE ##t
SQL queries must specify the columns of the result set. That's fundamental to SQL. Even PIVOT requires that your query specify the columns before you send it to the RDBMS.
For that reason, it's difficult and error-prone to create a query that returns rows as columns as you describe, and can adapt as needed to any number of columns.
Handling dynamic columns must be a two-stage procedure.
One option is to make the two stages be:
Write application code to build the SQL query dynamically, based on the distinct values found in the data. This requires an extra query to discover what values exist so you can build the query.
Execute the SQL query and retrieve the results.
The other option is to make the two stages be:
Run a more plain SQL query, that fetches rows as rows, as they are stored in the database.
Write application code to post-process the results, collecting individual values from rows into an expanding set of columns based on the values found. This does not require an extra query as the first design does.
Just join table A and B on B.DETAIL_ID == A.ID ?? Or is that too simple?

Select records with order of IN clause

I have
SELECT * FROM Table1 WHERE Col1 IN(4,2,6)
I want to select and return the records with the specified order which i indicate in the IN clause
(first display record with Col1=4, Col1=2, ...)
I can use
SELECT * FROM Table1 WHERE Col1 = 4
UNION ALL
SELECT * FROM Table1 WHERE Col1 = 6 , .....
but I don't want to use that, cause I want to use it as a stored procedure and not auto generated.
I know it's a bit late but the best way would be
SELECT *
FROM Table1
WHERE Col1 IN( 4, 2, 6 )
ORDER BY CHARINDEX(CAST(Col1 AS VARCHAR), '4,2,67')
Or
SELECT CHARINDEX(CAST(Col1 AS VARCHAR), '4,2,67')s_order,
*
FROM Table1
WHERE Col1 IN( 4, 2, 6 )
ORDER BY s_order
You have a couple of options. Simplest may be to put the IN parameters (they are parameters, right) in a separate table in the order you receive them, and ORDER BY that table.
The solution is along this line:
SELECT * FROM Table1
WHERE Col1 IN(4,2,6)
ORDER BY
CASE Col1
WHEN 4 THEN 1
WHEN 2 THEN 2
WHEN 6 THEN 3
END
select top 0 0 'in', 0 'order' into #i
insert into #i values(4,1)
insert into #i values(2,2)
insert into #i values(6,3)
select t.* from Table1 t inner join #i i on t.[in]=t.[col1] order by i.[order]
Replace the IN values with a table, including a column for sort order to used in the query (and be sure to expose the sort order to the calling application):
WITH OtherTable (Col1, sort_seq)
AS
(
SELECT Col1, sort_seq
FROM (
VALUES (4, 1),
(2, 2),
(6, 3)
) AS OtherTable (Col1, sort_seq)
)
SELECT T1.Col1, O1.sort_seq
FROM Table1 AS T1
INNER JOIN OtherTable AS O1
ON T1.Col1 = O1.Col1
ORDER
BY sort_seq;
In your stored proc, rather than a CTE, split the values into table (a scratch base table, temp table, function that returns a table, etc) with the sort column populated as appropriate.
I have found another solution. It's similar to the answer from onedaywhen, but it's a little shorter.
SELECT sort.n, Table1.Col1
FROM (VALUES (4), (2), (6)) AS sort(n)
JOIN Table1
ON Table1.Col1 = sort.n
I am thinking about this problem two different ways because I can't decide if this is a programming problem or a data architecture problem. Check out the code below incorporating "famous" TV animals. Let's say that we are tracking dolphins, horses, bears, dogs and orangutans. We want to return only the horses, bears, and dogs in our query and we want bears to sort ahead of horses to sort ahead of dogs. I have a personal preference to look at this as an architecture problem, but can wrap my head around looking at it as a programming problem. Let me know if you have questions.
CREATE TABLE #AnimalType (
AnimalTypeId INT NOT NULL PRIMARY KEY
, AnimalType VARCHAR(50) NOT NULL
, SortOrder INT NOT NULL)
INSERT INTO #AnimalType VALUES (1,'Dolphin',5)
INSERT INTO #AnimalType VALUES (2,'Horse',2)
INSERT INTO #AnimalType VALUES (3,'Bear',1)
INSERT INTO #AnimalType VALUES (4,'Dog',4)
INSERT INTO #AnimalType VALUES (5,'Orangutan',3)
CREATE TABLE #Actor (
ActorId INT NOT NULL PRIMARY KEY
, ActorName VARCHAR(50) NOT NULL
, AnimalTypeId INT NOT NULL)
INSERT INTO #Actor VALUES (1,'Benji',4)
INSERT INTO #Actor VALUES (2,'Lassie',4)
INSERT INTO #Actor VALUES (3,'Rin Tin Tin',4)
INSERT INTO #Actor VALUES (4,'Gentle Ben',3)
INSERT INTO #Actor VALUES (5,'Trigger',2)
INSERT INTO #Actor VALUES (6,'Flipper',1)
INSERT INTO #Actor VALUES (7,'CJ',5)
INSERT INTO #Actor VALUES (8,'Mr. Ed',2)
INSERT INTO #Actor VALUES (9,'Tiger',4)
/* If you believe this is a programming problem then this code works */
SELECT *
FROM #Actor a
WHERE a.AnimalTypeId IN (2,3,4)
ORDER BY case when a.AnimalTypeId = 3 then 1
when a.AnimalTypeId = 2 then 2
when a.AnimalTypeId = 4 then 3 end
/* If you believe that this is a data architecture problem then this code works */
SELECT *
FROM #Actor a
JOIN #AnimalType at ON a.AnimalTypeId = at.AnimalTypeId
WHERE a.AnimalTypeId IN (2,3,4)
ORDER BY at.SortOrder
DROP TABLE #Actor
DROP TABLE #AnimalType
ORDER BY CHARINDEX(','+convert(varchar,status)+',' ,
',rejected,active,submitted,approved,')
Just put a comma before and after a string in which you are finding the substring index or you can say that second parameter.
And first parameter of CHARINDEX is also surrounded by , (comma).