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 |
|
Lemmy44
Starting Member
20 Posts |
Posted - 2010-11-04 : 10:07:04
|
| I am trying to populate a table with a series of results, one of them being if a particular value exists (an ID number) in the database, to return a '1', and if it does not exist, then to return a '0'.I know this is probably a pretty simple process, but I have not had any experience with writing this type of query before.Thanks,Lem |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-08 : 05:24:33
|
| Can you give us more details? Also post the table structureMadhivananFailing to plan is Planning to fail |
 |
|
|
JonRussell
Starting Member
4 Posts |
Posted - 2010-11-09 : 13:33:36
|
Yes, I think we need more information here, but let me take a guess at what you are asking. Are you asking how to return a result based on the existence of a certain value in a column? If so, I think you are look for the CASE expression. http://msdn.microsoft.com/en-us/library/ms181765.aspxSELECT TOP 10 OrderId, AddressExists = CASE WHEN ISNULL(Address1, '') = '' THEN 0 ELSE 1 END FROM Orders In this example, a 0 is returned if the Address1 column is NULL, otherwise a 1 is returned.I hope that answers your question.Regards,Jon |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-10 : 10:23:09
|
or is it this you want?SELECT columns...,CASE WHEN l.IDField IS NULL THEN 0 ELSE 1 ENDFROM YourTable tLEFT JOIN LookupTable lON l.IDField=t.Field ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|