How to change Search query that contain particular word n Sql

karthik80c
karthik80c used Ask the Experts™
on
Hi Experts ,

We need to search in database with the record contains particular Data in it e.g. name column could search for all words that contains  'jon' in it. Is this correct query

my code
SELECT Name FROM Person WHERE Name LIKE '%Jon%'

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

yes it is correct.

It will look for the word where it contains "Jon" in the Name column.

Author

Commented:
Hi Lokesh ,

What if i need to go for exact match
Lokesh B RDeveloper
Top Expert 2015
Commented:
Hi,

Exact Match

SELECT Name FROM Person WHERE Name = 'Jon';

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
In this case you can run

SELECT Name FROM Person WHERE Name LIKE 'jon' 

Open in new window

It would work, but you need to change name at run time then below code will work

create table person (pid int identity primary key, name nvarchar(50))

insert into person (name)
values ('jon kumar')

insert into person (name)
values ('jon1 kumar')

insert into person (name)
values ('Ramjon kumar')

insert into person (name)
values ('Ram kumar')

DECLARE
	@Var nvarchar(100) = 'Jon'
SELECT
	*
FROM Person
WHERE name like '%' + @Var +'%'

Open in new window


Your note, database; in case you need to search in then LIKE would be good but for database; please use full text search.

Best Regards

Author

Commented:
Thank You Experts For Your Great Effort

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial