Solved

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

Posted on 2014-02-10
13
5,094 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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 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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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