SQL Subquery Question

Posted on 2012-08-30
Medium Priority
Last Modified: 2012-08-30
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.

Question by:jazzcatone
LVL 12

Assisted Solution

by:Anuradha Goli
Anuradha Goli earned 1000 total points
ID: 38350490
Select claim,CreateDate,
CASE WHEN RentalID is not null then 'Y'
else 'N' end as Rental
from Claim

Open in new window

LVL 13

Accepted Solution

LIONKING earned 1000 total points
ID: 38350518
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


Author Closing Comment

ID: 38350533
Thank You !!!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question