Solved

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

Posted on 2010-09-17
10
339 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Resume 5 45
SQL Server Express or Standard? 5 25
Related to SQL Query 5 20
Database Mail Profiles 1 20
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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

756 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