General discussion


Can Like do this in a SQL query?

By PWS442 ·
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?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

SQL Server solution

by Glen_McLeod In reply to Can Like do this in a SQL ...

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.


Related Discussions

Related Forums