Need help with CASE statement

Posted on 2010-01-06
Last Modified: 2012-05-08
I have read CASE syntax, but I am still confuse.  I want to write a CASE base on targetID as follow:

targetID = 1
FabricToSend="1 yard"

targetID = 2
FabricToSend = "2 yard"

i have no clue on how to start. :(  
can someone guide me?

Question by:aspnewbie09
    LVL 75

    Accepted Solution

    SELECT FabricToSend = CASE (TargetID) when 1 then '1 yard' when 2 then '2 Yard' else null end


    SELECT  FabricToSend =  = CAST (TargetID as varchar)+ ' yard'
    LVL 67

    Expert Comment

    The latter needs to be
    SELECT  FabricToSend = CAST (TargetID as varchar)+ ' yard'
    of course (single equal sign).
    And CASE works without those parens, they are not needed.
    LVL 26

    Expert Comment

    SELECT CASE WHEN targetID = 1 THEN '1 yard'
                      WHEN targetID = 2 THEN '2 yard'
                      ELSE ''
                END AS FabricToSend

    Author Closing Comment

    instruction not accurate.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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…

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now