Link to home
Start Free TrialLog in
Avatar of danielolorenz
danielolorenz

asked on

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

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
Avatar of dannygonzalez09
dannygonzalez09

what is the issue.. Sry, but i don't see any string concatenations in the case
Avatar of Surendra Nath
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?
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
Avatar of danielolorenz

ASKER

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

I want to retrieve an ErrorDescription column results.

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

Dan
The case statement will end upon matching one item...
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.
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),
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
CORRECTION:

SUBSTRING(..., 3, 8000)
ASKER CERTIFIED SOLUTION
Avatar of danielolorenz
danielolorenz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Exactly the way I did in the code I posted.
Great