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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using IF EXISTS

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 structure

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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.aspx
SELECT 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
Go to Top of Page

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 END
FROM YourTable t
LEFT JOIN LookupTable l
ON l.IDField=t.Field


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -