?
Solved

ms sql 2005 selecting records with specific values in a field

Posted on 2007-10-19
1
Medium Priority
?
167 Views
Last Modified: 2010-03-20
I have table1 with a field fullname

I could do:

select * from table1 where fullname like '%david%' or fullname like'%john%' or fullname like '%mary%'

when the names are many in the where part itbecomes very impractical

I have a names table  with a field name

say

david
john
mary

I need a select statement equivalent to the above but simpler, useng the names table like



0
Comment
Question by:robrodp
1 Comment
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 20108311
Hello robrodp,

Here goes.  With a lot of records, this will be *slow*:

SELECT DISTINCT t.*
FROM table1 t INNER JOIN
    names n ON CHARINDEX(n.[name], t.fullname) > 0

The DISTINCT is in there so that you do not get multiple result records when >1 item from the names
table is a match for a given fullname.

Regards,

Patrick
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question