Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so:
SELECT text
FROM table_1
WHERE ID LIKE '%SELECT ID FROM table_2%'
I'm looking for product ids within a large body of text that is held in a DB. The SELECT statement in the wildcard clause will return 50+ rows. The statement above is obviously not the way to go. Any suggestions?
You can do a join and construct the like string based on table_2.
SELECT * FROM table_1 t1
INNER JOIN table_2 t2 ON t1.ID LIKE '%' + CONVERT(VARCHAR, t2.ID) + '%'
Related
I have two tables in database.
Both tables have a business name column but not always going to be the same.
For example tbl 1 has a business name of 'Aone Dental Practices Limited TA Jaws Dental' and Tbl 2 has a business name of 'Jaws Dental'. I want to be able to join these together as Jaws Dental is visible in both.
I can't seem to get the Like clause working for this.
tried
Tbl1_BusinesName Like '%' + Tbl2_BusinesName + '%'
This query should work :
SELECT *
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.BusinesName LIKE '%'+TS.BusinesName+'%'
Using EXISTS you can get the expected result:
SELECT *
FROM dbo.TableName1 AS Tbl1
WHERE EXISTS (SELECT 1
FROM dbo.TableName2 AS Tbl2
WHERE Tbl1.BusinesName LIKE '%' + Tbl2.BusinesName + '%');
I am writing a DB2 Stored procedure where I need to have a condition that select all values from table 1 and table 2 where table1.column_a starts with table2.column_b.
select * from table1 T1, table2 T2 where T1.column_a like T2.column_b + '%'
I tried playing around the above SQL but it seems like an invalid SQL. Any suggestions?
You can use the concat() function or || concatenation operator. + for string concatenation is used by SQL Server and similar databases.
I would phrase this as a join:
select *
from table1 T1 join
table2 T2
on T1.column_a like concat(T2.column_b, '%');
Consider that column_a and column_b has trailing blanks and that there is an issue with like operand. You could always use left() and combine it with RTRIM(). I've have very limited experience with DB2 but this is my hunch.
select *
from table1 T1
INNER JOIN table2 T2
on LEFT(RTRIM(T1.column_a), LENGTH(RTRIM(T2.column_b))) = T2.column_b
How to write a SQL like query with table column ?
I have two tables table1 and table2.
Table1 has Notes
Table2 has billid
We have around 100 bills which satisfies Like condition.
Right now, i am writing with Static value on the column.
How should i write a Like Query by passing Column in the Like Condition.
select * from table1
where Notes like select billid from table2
You are wanting a JOIN here.
If the exact BillID is found in Table1.Column1 then:
select table1.*
from table1
INNER JOIN table2 ON
table1.column1=table2.billid
You could also do this using IN in the WHERE clause if you truly don't need any records from your table2 in the result set:
select *
from table1
where column1 in (SELECT billid FROM table2);
Which is pretty close to what you were aiming for in your attempted query.
Lastly, if you actually mean LIKE which is more of a wildcard match in SQL terms where the column1 merely has to contain a billid then back to the join:
select table1.*
from table1
INNER JOIN table2 ON
table1.column1 LIKE '%' + table2.billid + '%'
Try this. Select a raw from table1 if any match from table2 exists
select *
from table1
where exists (select billid
from table2
where table1.column1 LIKE '%' + table2.billid + '%')
I know this question has been asked, but I have a slightly different flavour of it. I have a use case where the only thing I have control over is the WHERE clause of the query, and I have 2 tables.
Using simple example:
Table1 contains 1 column named "FULLNAME" with hundreds of values
Table2 contains 1 column named "PATTERN" with some matching text
so, What I need to do is select all values from Table 1 which match the values in table 2.
Here's a simple example:
Table1 (FULLNAME)
ANTARCTICA
ANGOLA
AUSTRALIA
AFRICA
INDIA
INDONESIA
Table2 (PATTERN)
AN
IN
Effectively what I need is the entries in Table1 which contain the values from Table2 (result would be ANTARCTICA, ANGOLA, INDIA, INDONESIA)
In other words, what I need is something like:
Select * from Table1 where FULLNAME IN LIKE (Select '%' || Pattern || '%' from
Table2)
The tricky thing here is I only have control over the where clause, I can't control the Select clause at all or add joins since I'm using a product which only allows control over the where clause. I can't use stored procedures either.
Is this possible?
I'm using Oracle as the backend DB
Thanks
One possible approach is to use EXISTS in combination with LIKE in the subquery:
select * from table1 t1
where exists (select null
from table2 t2
where t1.fullname like '%' || t2.pattern || '%');
I believe that you can do this with a simple JOIN:
SELECT DISTINCT
fullname
FROM
Table1 T1
INNER JOIN Table2 T2 ON T1.fullname LIKE '%' || T2.pattern || '%'
The DISTINCT is there for those cases where you might have a match to multiple rows in Table2.
If the patterns are always two characters and only have to match the start of the full name, like the examples you showed, you could do:
Select * from Table1 where substr(FULLNAME, 1, 2) IN (Select Pattern from Table2)
Which prevents any index on Table1 being used, and your real case may need to be more flexible...
Or probably even less efficiently, similar to TomH's approach, but with the join inside a subquery:
Select * from Table1 where FULLNAME IN (
Select t1.FULLNAME from Table1 t1
Join Table2 t2 on t1.FULLNAME like '%'||t2.Pattern||'%')
Right, this involved a bit of trickery. Conceptually what I've done is turned the column from the PATTERN into a single cell, and use that with REGEX_LIKE
So the values "AN and IN" becomes one single value '(AN|IN)' - I just feed this to the regexp_like
SELECT FULLNAME from table1 where
regexp_like(FULLNAME,(SELECT '(' || SUBSTR (SYS_CONNECT_BY_PATH (FULLNAME , '|'), 2) || ')' Table2
FROM (SELECT FULLNAME , ROW_NUMBER () OVER (ORDER BY FULLNAME) rn,
COUNT (*) OVER () cnt
FROM Table2)
WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = PRIOR rn + 1))
The subquery in the regexp_like turns the column into a single cell containing the regular expression string.
I do realise this is probably a performance killer though, but thankfully I'm not that fussed about performance at this point
I am aware of using where and like this
select * from Table1
join table2
on table1.column like %text%
i want to extend the same logic to the column. i.e
select * from Table1
join table2
on table1.column like %table2.column%
This is impossible on SQL? is there an alternative for this?
Thanks
Try to get into the habit of always specifying the DB engine you are using and including sample data and expected output based on that sample data. In this case the sample data etc can help us provide a more relevant answer. there may be a better solution to your problem than using like in your join clause.
but for the simple example you have given the below should work.
select * from Table1
join table2
on table1.column like '%' + table2.column + '%'
EDIT
From this resource it seems proc-sql uses double pipe || for string contatenation and double quotes for strings so try the new version below.
select * from Table1
join table2
on table1.column like "%" || table2.column || "%"
Try this
select * from Table1
join table2
on table1.column like '%' + table2.column + '%' COLLATE Latin1_General_CS_AS
where (COLLATE Latin1_General_CS_AS) stands for case sensitive in Microsoft SQL Server