?
Solved

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

Posted on 2014-02-10
13
Medium Priority
?
5,318 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 70

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 70

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 70

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 70

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

750 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