Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 simplifying a UNION statement

Author  Topic 

tariq2
Posting Yak Master

125 Posts

Posted - 2011-10-31 : 13:34:38
Hi,
Thank you for your help.
Is there a means of simplifying the following UNION
statement?


select * from D_Vendors where VendorName like '%Criteria%'
UNION
select * from D_Vendors where VendorName like '%Alistair-Hugo%'
UNION
select * from D_Vendors where VendorName like '%Elizabeth Higgins%'
UNION
select * from D_Vendors where VendorName like '%Focus Vision%'
UNION
select * from D_Vendors where VendorName like '%Redhead%'
UNION
select * from D_Vendors where VendorName like '%Research House%'
UNION
select * from D_Vendors where VendorName like '%Spectrum%'
UNION
select * from D_Vendors where VendorName like '%Take Note%'
UNION
select * from D_Vendors where VendorName like '%Transcription Global%'
UNION
select * from D_Vendors where VendorName like '%Viewpoint%'
UNION
select * from D_Vendors where VendorName like '%White Rooms%'
UNION
select * from D_Vendors where VendorName like '%Wyoming%'
)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-31 : 13:40:55
best way to do this is to pass the string values as a comma seperated list like

DECLARE @SearchList varchar(1000)
SET @SearchList='Criteria,Alistair-Hugo,Elizabeth Higgins,...,Wyoming'

and use like

SELECT d.*
FROM D_Vendors d
INNER JOIN dbo.ParseValues(@SearchList,',')f
ON d.VendorName LIKE '%' + f.Val + '%'


ParseValues can be found in below link

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-10-31 : 13:41:36
select * from D_Vendors
where VendorName like '%Criteria%'
or VendorName like '%Alistair-Hugo%'
or VendorName like '%Elizabeth Higgins%'
...

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -