SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select Where Any Column Equals (or Like) Value


Select Where Any Column Equals (or Like) Value

Author
Message
tommy.becker
tommy.becker
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 8
Great idea, but you either need to handle nulls ... isnull({{column}},'') or cast them as strings... Found this out the hard way.
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23425 Visits: 9730
If you want to test equality conditions, you can do this:

Where @Parameter in (Col1, Col2, Col3)



That will find any column that is equal to the parameter value.

Can't do that so easily with Like instead of equality.

If it's a whole word, not a word fragment, you can find this kind of thing with full text indexing. Like "Fred" in "Fred and Wilma", not like "bam" in "Bambam".

Edit: And you'll definitely want to use IsNull or something similar if you concatenate columns together. That's been mentioned, but I thought I'd just reinforce that statement.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Peter Brinkhaus
Peter Brinkhaus
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2024 Visits: 7369
I know this is an old thread, but just in case someone is still interested, here's a solution without explicitly referencing all columns and no dynamic SQL.


CREATE TABLE #Test
(
Name VARCHAR(100),
Age INT,
Description VARCHAR(100),
Field1 INT,
Field2 VARCHAR(100)
)
GO

INSERT INTO
#Test
VALUES
('Tom', 30, 'Something', 1, 'ghjk'),
('Jen', 23, 'Something', 2, 'fjfor'),
('Jim', 22, 'Something', 3, 'ghop'),
('Tom', 35, NULL, 4, 'asop')

DECLARE @SearchString VARCHAR(MAX)
SET @SearchString = '%gh%'

SELECT
*
FROM
#Test T
WHERE
EXISTS
(
SELECT
*
FROM
(
SELECT T.* FOR XML PATH, TYPE
) X(C) -- Turn all columns into XML
CROSS APPLY
X.C.nodes('//*/.') E(V) -- Get all XML elements
WHERE
E.V.value('./text()[1]', 'VARCHAR(MAX)') LIKE @SearchString -- Filter on value of the XML element
)

DROP TABLE #Test



Might be very slow on large tables because each row has to be composed into XML before filtering.

P.S. Tested on SQL 2008 R2, but I suppose this works on SQL 2005 as well
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search