SQL Subquery Question

Posted on 2012-08-30
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
    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

    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

    Thank You !!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now