Solved

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

Posted on 2014-02-10
13
4,897 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
 

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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:ScottPletcher
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:ScottPletcher
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:ScottPletcher
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:ScottPletcher
ID: 39850645
Exactly the way I did in the code I posted.
0
 

Author Closing Comment

by:danielolorenz
ID: 39862463
Great
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

25 Experts available now in Live!

Get 1:1 Help Now