Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
concoran
Starting Member
7 Posts |
Posted - 2010-03-05 : 01:13:33
|
| Is there a to find if a certain value exists in a column? For e.g, if zipcode is a column, what is the best way to find if a certain zipcode exists in that column?TIA |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2010-03-05 : 01:43:13
|
| if exists (select * from tablename where zipcode = value)select 'already exists'else select 'no record' |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2010-03-05 : 01:46:08
|
| [code]DECLARE @ZipCode varchar(50)if not exists (select * from TestTable where ZipCode = @ZipCode)begin ...Do something here!!endelse PRINT 'ZipCode already exist'[/code] |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2010-03-05 : 01:50:07
|
quote: Originally posted by bklr if exists (select * from tablename where zipcode = value)select 'already exists'else select 'no record'
Its better to avoid select *. so the statement looks like thisif exists (select 1 from tablename where zipcode = value)select 'already exists'else select 'no record' |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-05 : 02:37:31
|
| I am confused which is faster SELECT 1 or SELECT NULL in exists bracket.because i always use null in this scenario.Vabhav T |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
|
|
|
|
|