• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Check to see if values exist

How do I check to see if the value "25" exists or not in a column named "Product"?  
0
johnqtr
Asked:
johnqtr
  • 2
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
SELECT Count(Product)
FROM YourTable
WHERE Product = 25

(if Product is text)
WHERE Product = '25'

@@ROWCOUNT will now have the number of records in the above query, so you can test it.

Hope this helps.
-Jim
0
 
rafranciscoCommented:
SELECT * FROM YourTable
WHERE Product = 25

or

SELECT 'Product 25 Exists' FROM YourTable
WHERE EXISTS (SELECT 'X' FROM YourTable
                        WHERE Product = 25)
0
 
rafranciscoCommented:
SELECT 'Product 25 Exists'
WHERE EXISTS (SELECT 'X' FROM YourTable
                        WHERE Product = 25)
0
 
adatheladCommented:
IF EXISTS(SELECT 1 FROM TableName WHERE Product = 25)
    -- Product exists so do something...
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks.  IMO rafrancisco and adathelad's comments are equally workable...
0

Featured Post

Industry Leaders: 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!

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