Solved

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

Posted on 2010-09-17
10
335 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

21 Experts available now in Live!

Get 1:1 Help Now