Solved

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

Posted on 2014-02-10
13
5,157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 5

Expert Comment

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

Expert Comment

by:Surendra Nath
ID: 39847963
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
ID: 39847974
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

Author Comment

by:danielolorenz
ID: 39847986
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
ID: 39847991
I want to retrieve an ErrorDescription column results.

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

Dan
0
 

Expert Comment

by:cyimxtck
ID: 39847995
The case statement will end upon matching one item...
0
 

Expert Comment

by:cyimxtck
ID: 39848008
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:Scott Pletcher
ID: 39848107
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:Scott Pletcher
ID: 39848113
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:Scott Pletcher
ID: 39848119
CORRECTION:

SUBSTRING(..., 3, 8000)
0
 

Accepted Solution

by:
danielolorenz earned 0 total points
ID: 39850520
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:Scott Pletcher
ID: 39850645
Exactly the way I did in the code I posted.
0
 

Author Closing Comment

by:danielolorenz
ID: 39862463
Great
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

724 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