Discussion Locked

Can Like do this in a SQL query?

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to find all rows of a table where a field is contained in another table. Table1.Col1 is a 1 character field. Table2.Col2 is a string that will contain one or more characters from Table1.Col1. If three rows in Table1 contain "A", "B", and "C",and one row in Table2 contains "AC", I would want to return the 1st and 3rd rows from Table1.

How do I do this?

1 reply

by glen_mcleod

Here's what you do if using SQL Server:

Select Table1.Col1
From Table1 inner join Table2 on
Table1.col1 = SUBSTRING(Table2.Col2, CHARINDEX(Tabel1.Col1, table2.Col2), 1)

CHARINDEX finds the location of Col1 inside Col2, returning 0 if not found. SUBSTRING takes the location from CharIndex and extracts 1 character and uses it for the join.

These 2 functions (CHARINDEX and SUBSTRING) are for T_SQL (SQL Server), but your DBMS should provide their equivalents.

Glen

This discussion is closed to new replies.