Solved

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

Posted on 2010-09-17
10
336 Views
Last Modified: 2012-05-10
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
Comment
Question by:rayluvs
10 Comments
 
LVL 7

Accepted Solution

by:
tlovie earned 125 total points
ID: 33704068
I think this would work:

select * from Items where Itemcode in ('okn123', 'OKI987987', 'pop9090')
0
 
LVL 16

Assisted Solution

by:vdr1620
vdr1620 earned 125 total points
ID: 33704110
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
 
LVL 12

Assisted Solution

by:jagssidurala
jagssidurala earned 125 total points
ID: 33704224

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
 
LVL 15

Assisted Solution

by:AmmarR
AmmarR earned 125 total points
ID: 33704280
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:rayluvs
ID: 33705130
Great, thanx all!
0
 

Author Comment

by:rayluvs
ID: 33705185
Somehow I can reward the points, its saying that I am closing.

please advice
0
 

Author Comment

by:rayluvs
ID: 33705417
Thats exactly what we are doing, but will try again.
0
 

Author Comment

by:rayluvs
ID: 33705438
I dont see that it was rewarded, its asking for a comment to close
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now