You are here: Re: LIKE and IN can I combine « MsSQL Server « IT news, forums, messages
Re: LIKE and IN can I combine

Posted by Razvan Socol on 10/10/05 20:19

Hello, Rob

Instead of this:

SELECT * FROM employees
WHERE LastName LIKE 'C%'
OR LastName LIKE 'D%'
OR LastName LIKE 'F%'

you can use:

SELECT e.* FROM employees e INNER JOIN (
SELECT 'C' AS Prefix
UNION ALL SELECT 'D'
UNION ALL SELECT 'F'
) x ON e.LastName LIKE x.Prefix+'%'

or:

SELECT * FROM employees e
WHERE EXISTS (
SELECT * FROM (
SELECT 'C' AS Prefix
UNION ALL SELECT 'D'
UNION ALL SELECT 'F'
) x WHERE e.LastName LIKE x.Prefix+'%'
)

The last query is more correct than the previous (in case that two
prefixes could match the same name).

Of course, it's better to use a temporary table to store all the
requested prefixes.

Razvan

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация