A SQL script to search constant with a table in Ms SQL

We need to search various constant value of a column within a table.

The constants are 'okn123', 'OKI987987', 'pop9090' in the Itemcode column of the table.

We want to know if exist in the table
rayluvsAsked:
Who is Participating?
 
tlovieConnect With a Mentor Commented:
I think this would work:

select * from Items where Itemcode in ('okn123', 'OKI987987', 'pop9090')
0
 
vdr1620Connect With a Mentor Commented:
You can do it various ways like

SELECT * FROM TableName
WHERE ItemCode = 'okn123' OR ItemCode =  'OKI987987' OR ItemCode =  'pop9090'

OR

SELECT * FROM TableName
WHERE ItemCode IN ('okn123', 'OKI987987', 'pop9090')
0
 
jagssiduralaConnect With a Mentor Commented:

Declare @Itemcode nvarchar(32)

Select  *  
From    tblItems
Where itemcode like @Itemcode

Here @Itemcode  is a variable by this you can send any of the item code, it will search for the required items.

if you want exact match change the where condition in above query .

Where itemcode = @Itemcode

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
AmmarRConnect With a Mentor Commented:
all methods above seems to do the job, here below is just another way


SELECT *
FROM table
WHERE 
(PATINDEX('okn123', ItemCode ) > 0)
 OR 
(PATINDEX('OKI987987', ItemCode ) > 0)
or
(PATINDEX('pop9090', ItemCode ) > 0)

Open in new window

0
 
rayluvsAuthor Commented:
Great, thanx all!
0
 
rayluvsAuthor Commented:
Somehow I can reward the points, its saying that I am closing.

please advice
0
 
rayluvsAuthor Commented:
Thats exactly what we are doing, but will try again.
0
 
rayluvsAuthor Commented:
I dont see that it was rewarded, its asking for a comment to close
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.