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

SQL Subquery Question

Select claim,CreateDate,
IF (SELECT RentalID FROM claim is not null)'Y' ELSE 'N') as Rental 
from Claim

Open in new window

OK this seems like it should be simple. Seems like I've done this before but for whatever reason its not registering today. I have a query that brings back a lot of data. In the query I want to look at the value (or a lack thereof)  of a column called "RentalID". Depending on the value of "RentalID" I want to generate another column called "Rental"

The column called "Rental" will be a yes/no column. If there is a value for rentalID then rental will be equal to "Yes". If rentalID is null or blank then rental will be equal to "No"

How to write? It seems like I need some sort of a subquery or perhaps an exists statement but I cannot seem to find the correct syntax. My query attempt is below. Any help is most appreciated.

Jason
0
jazzcatone
Asked:
jazzcatone
2 Solutions
 
Anuradha GoliSystems Development / Support SpecialistCommented:
Select claim,CreateDate,
CASE WHEN RentalID is not null then 'Y'
else 'N' end as Rental
from Claim

Open in new window

0
 
LIONKINGCommented:
anuradhay's solution does the trick, just adding an ISNULL clause to handle empty values, and changing the Y and N values.

In this case, if RentalID is Null or Empty it displays No, otherwise it displays Yes.

Select claim,CreateDate,
CASE WHEN LTRIM(RTRIM(ISNULL(RentalID,'')))='' then 'No'
else 'Yes' end as Rental
from Claim

Open in new window

0
 
jazzcatoneAuthor Commented:
Thank You !!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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