Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

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
0
rayluvs
Asked:
rayluvs
4 Solutions
 
tlovieCommented:
I think this would work:

select * from Items where Itemcode in ('okn123', 'OKI987987', 'pop9090')
0
 
vdr1620Commented:
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
 
jagssiduralaCommented:

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AmmarRCommented:
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now