ms sql 2005 selecting records with specific values in a field

Posted on 2007-10-19
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



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

Question by:robrodp
    1 Comment
    LVL 92

    Accepted Solution

    Hello robrodp,

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

    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.



    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now