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
 Search the database table for a Keyword! PLS HELP!

Author  Topic 

jimdarter
Starting Member

14 Posts

Posted - 2010-05-10 : 16:13:03
Dear All,

I have a database which has only ONE table and I want to write a query that will search for any keyword that has been entered by the user on a web-front interface. Can you pls help me with this?

Your help is highly appreciated.

Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-10 : 16:15:06
DECLARE @SomeVar varchar(50)
SET @SomeVar = 'SomeString'
SELECT * FROM YourTable WHERE SomeColumn LIKE '%' + @SomeVar + '%'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jimdarter
Starting Member

14 Posts

Posted - 2010-05-10 : 16:17:42
quote:
Originally posted by tkizer

DECLARE @SomeVar varchar(50)
SET @SomeVar = 'SomeString'
SELECT * FROM YourTable WHERE SomeColumn LIKE '%' + @SomeVar + '%'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Well that is the whole problem, I want to search all the columns and not just one particular columns in the table. Thanks for your prompt reply.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-10 : 16:23:45
DECLARE @SomeVar varchar(50)
SET @SomeVar = 'SomeString'
SELECT * FROM YourTable WHERE SomeColumn1 LIKE '%' + @SomeVar + '%' OR SomeColumn2 LIKE '%' + @SomeVar + '%' OR...

You can loop through the columns in INFORMATION_SCHEMA.COLUMNS, however it would be better to hard-code each of them. You only want to search through the character data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jimdarter
Starting Member

14 Posts

Posted - 2010-05-10 : 16:28:12
quote:
Originally posted by tkizer

DECLARE @SomeVar varchar(50)
SET @SomeVar = 'SomeString'
SELECT * FROM YourTable WHERE SomeColumn1 LIKE '%' + @SomeVar + '%' OR SomeColumn2 LIKE '%' + @SomeVar + '%' OR...

You can loop through the columns in INFORMATION_SCHEMA.COLUMNS, however it would be better to hard-code each of them. You only want to search through the character data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



I think that is a good way to do it but I don't know if it will work for the columns that are Integer based. Let me try it though. Thanks for the suggestion.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-10 : 16:53:09
That's why I said it'll work for the character data. You'll need to define the business requirement for numeric data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -