Solved

How Do You Write an SQL Case Statement With Multiple Conditions?

Posted on 2014-02-10
13
4,825 Views
Last Modified: 2014-02-16
I am wring an SQL case statement using multiple conditions with string concatenation.  My case statement is not working properly.  

Here is the code:
ErrorDescription = CASE WHEN LTRIM(RTRIM(ISNULL(EventIdentifier, ''))) = ''
                                        THEN 'EventIdentifier Missing, '
										WHEN LEN(LTRIM(RTRIM(PrimaryDrugNDC))) > 11
											   THEN 'PrimaryDrugNDC Too Long, '
										WHEN LTRIM(RTRIM(ISNULL(SecondaryDrugName, ''))) = ''
											   THEN 'SecondaryDrugName Missing, '
										WHEN LEN(LTRIM(RTRIM(SecondaryDrugName))) > 150
											   THEN 'SecondaryDrugName Too Long, '
										WHEN LTRIM(RTRIM(ISNULL(SecondaryDrugNDC, ''))) = ''
											   THEN 'SecondaryDrugNDC Missing, '
						ELSE ''
                 END,

Open in new window

                               


Thanks,

Dan
0
Comment
Question by:danielolorenz
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 5

Expert Comment

by:dannygonzalez09
Comment Utility
what is the issue.. Sry, but i don't see any string concatenations in the case
0
 
LVL 16

Expert Comment

by:Surendra Nath
Comment Utility
My case statement is not working properly.

can you tell us what is happening currently with your case statement and what exactly you need it to happen?
0
 

Expert Comment

by:cyimxtck
Comment Utility
create table t_value
(
      x            tinyint                  
)

insert into t_value
(
      x
)

values
(
 3
)

select
  case
  when x = 2            then 'two'
  when x = 3            then 'three'
  end
from
  t_value
0
 

Author Comment

by:danielolorenz
Comment Utility
The case statement is returning blanks.

Here is an example of my case statement when I use string concatenation:

Case Statement:
ErrorDescription = CASE WHEN LTRIM(RTRIM(ISNULL(EventIdentifier,
                                                              ''))) = ''
                                        THEN 'EventIdentifier Missing, '
                                   END
                + CASE WHEN LEN(LTRIM(RTRIM(EventIdentifier))) > 20
                       THEN 'EventIdentifier Too Long, '
                  END
                + CASE WHEN LTRIM(RTRIM(ISNULL(EventDate, ''))) = ''
                       THEN 'EventDate Missing, '
                  END
                + CASE WHEN LEN(LTRIM(RTRIM(EventDate))) > 25
                       THEN 'EventDate Too Long, '
                  END,

Open in new window

0
 

Author Comment

by:danielolorenz
Comment Utility
I want to retrieve an ErrorDescription column results.

For Example: "EventIdentifier Too Long, EventDate Missing,"

Dan
0
 

Expert Comment

by:cyimxtck
Comment Utility
The case statement will end upon matching one item...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:cyimxtck
Comment Utility
What you could do here is create a table with all the possible options and populate it with a dictionary pair:

x       Y
val     3

Select those values out in an inner join to the Y value, retrieve the X value and concatenate them.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Your last post is very close.  I suggest using leading ", " instead of trailing, as it allows a standard SUBSTRING to give a final result:

 ErrorDescription = SUBSTRING(
                    CASE WHEN LTRIM(RTRIM(ISNULL(EventIdentifier, ''))) = ''
                         THEN ', EventIdentifier Missing' ELSE ''
                    END
                  + CASE WHEN LEN(LTRIM(RTRIM(EventIdentifier))) > 20
                         THEN ', EventIdentifier Too Long' ELSE ''
                    END
                  + CASE WHEN LTRIM(RTRIM(ISNULL(EventDate, ''))) = ''
                         THEN ', EventDate Missing' ELSE ''
                    END
                  + CASE WHEN LEN(LTRIM(RTRIM(EventDate))) > 25
                         THEN ', EventDate Too Long' ELSE ''
                    END
                    , 2, 8000),
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
For example:


SELECT  
    EventIdentifier,EventDate,              
 ErrorDescription = SUBSTRING(
                    CASE WHEN LTRIM(RTRIM(ISNULL(EventIdentifier, ''))) = ''
                         THEN ', EventIdentifier Missing' ELSE ''
                    END
                  + CASE WHEN LEN(LTRIM(RTRIM(EventIdentifier))) > 20
                         THEN ', EventIdentifier Too Long' ELSE ''
                    END
                  + CASE WHEN LTRIM(RTRIM(ISNULL(EventDate, ''))) = ''
                         THEN ', EventDate Missing' ELSE ''
                    END
                  + CASE WHEN LEN(LTRIM(RTRIM(EventDate))) > 25
                         THEN ', EventDate Too Long' ELSE ''
                    END
                    , 2, 8000)
FROM (
    SELECT '' AS EventIdentifier, 'this is toooooo long for the dateeeeeeeeeeeeeeeeeeeee.' AS EventDate UNION ALL
    SELECT 'this is a veryyyyyyy longgggg valueeeeeee.', '' UNION ALL
    SELECT NULL, NULL UNION ALL
    SELECT 'these values should', 'be just right.'
) AS test_data
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
CORRECTION:

SUBSTRING(..., 3, 8000)
0
 

Accepted Solution

by:
danielolorenz earned 0 total points
Comment Utility
I added an ELSE '' to the CASE STATEMENTS and it works fine now.

This is the solution I was looking for:
               ErrorDescription = CASE WHEN LEN(LTRIM(RTRIM(EventIdentifier))) > 20
                                                         THEN 'EventIdentifier Too Long, '
                                                         ELSE ''
                                                  END
                                                + CASE WHEN LTRIM(RTRIM(ISNULL(EventDate, ''))) = ''
                                                         THEN 'EventDate Missing, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(EventDate))) > 25
                                                         THEN 'EventDate Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(Username))) > 20
                                                         THEN 'Username Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(FirstName))) > 50
                                                         THEN 'FirstName Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(LastName))) > 50
                                                            THEN 'LastName Too Long, '
                                                            ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(Location))) > 100
                                                            THEN 'Location Too Long, '
                                                            ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(InterventionName))) > 150
                                                            THEN 'InterventionName Too Long, '
                                                            ELSE ''
                                                END
                                                + CASE WHEN LTRIM(RTRIM(ISNULL(Accepted, ''))) = ''
                                                         THEN 'Accepted Missing, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(Accepted))) > 1
                                                         THEN 'Accepted Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LTRIM(RTRIM(ISNULL(PrimaryDrugName, ''))) = ''
                                                         THEN 'PrimaryDrugName Missing, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(PrimaryDrugName))) > 150
                                                         THEN 'PrimaryDrugName Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(PrimaryDrugNDC))) > 11
                                                         THEN 'PrimaryDrugNDC Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(SecondaryDrugName))) > 150
                                                         THEN 'SecondaryDrugName Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(SecondaryDrugNDC))) > 11
                                                         THEN 'SecondaryDrugNDC Too Long, '
                                                         ELSE ''
                                                END
                                                + CASE WHEN LEN(LTRIM(RTRIM(Comments))) > 2000
                                                         THEN 'Comments Too Long, '
                                                         ELSE ''
                                                END
                                    + '::END',
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Exactly the way I did in the code I posted.
0
 

Author Closing Comment

by:danielolorenz
Comment Utility
Great
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now