Solved

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

Posted on 2010-09-17
10
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

695 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