devxlogo

SELECT Using LIKE

SELECT Using LIKE

Question:
Can I use LIKE across all columns in a single table without having to name each column specifically?

Answer:
You cannot do this as a direct query against a table, but you could create a view that concatenates the values in the columns and then query that view. If you have a table like this:

TestTableCol1 char(10)Col2 char(10)Col3 char(10)

And it has these values:

Col1 Col2 Col3a    test btest c    de    f    g

You can create a view called TestView that combines columns 1, 2, and 3:

if exists (select * from sysobjects where id = 
object_id(N'[dbo].[TestView]') and
OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[TestView]GOSET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GOCREATE VIEW dbo.TestViewASSELECT Col1 + Col2 + Col3 AS testcolFROM TestTableGOSET QUOTED_IDENTIFIER OFF SET ANSI_NULLS ON GO

Then you can query the view like this:

select * from testview where testcol like '%test%'

And it will return this:

testcol                        ------------------------------ a         test      b         test      c         d

See also  5 Tips for Working With an Onsite Interpreter
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist