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:
Thanks,
Dan
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,
Thanks,
Dan
what is the issue.. Sry, but i don't see any string concatenations in the case
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
(
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
ASKER
The case statement is returning blanks.
Here is an example of my case statement when I use string concatenation:
Case Statement:
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,
ASKER
I want to retrieve an ErrorDescription column results.
For Example: "EventIdentifier Too Long, EventDate Missing,"
Dan
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.
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(EventId entifier, ''))) = ''
THEN ', EventIdentifier Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventIdent ifier))) > 20
THEN ', EventIdentifier Too Long' ELSE ''
END
+ CASE WHEN LTRIM(RTRIM(ISNULL(EventDa te, ''))) = ''
THEN ', EventDate Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventDate) )) > 25
THEN ', EventDate Too Long' ELSE ''
END
, 2, 8000),
ErrorDescription = SUBSTRING(
CASE WHEN LTRIM(RTRIM(ISNULL(EventId
THEN ', EventIdentifier Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventIdent
THEN ', EventIdentifier Too Long' ELSE ''
END
+ CASE WHEN LTRIM(RTRIM(ISNULL(EventDa
THEN ', EventDate Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventDate)
THEN ', EventDate Too Long' ELSE ''
END
, 2, 8000),
For example:
SELECT
EventIdentifier,EventDate,
ErrorDescription = SUBSTRING(
CASE WHEN LTRIM(RTRIM(ISNULL(EventId entifier, ''))) = ''
THEN ', EventIdentifier Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventIdent ifier))) > 20
THEN ', EventIdentifier Too Long' ELSE ''
END
+ CASE WHEN LTRIM(RTRIM(ISNULL(EventDa te, ''))) = ''
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
SELECT
EventIdentifier,EventDate,
ErrorDescription = SUBSTRING(
CASE WHEN LTRIM(RTRIM(ISNULL(EventId
THEN ', EventIdentifier Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventIdent
THEN ', EventIdentifier Too Long' ELSE ''
END
+ CASE WHEN LTRIM(RTRIM(ISNULL(EventDa
THEN ', EventDate Missing' ELSE ''
END
+ CASE WHEN LEN(LTRIM(RTRIM(EventDate)
THEN ', EventDate Too Long' ELSE ''
END
, 2, 8000)
FROM (
SELECT '' AS EventIdentifier, 'this is toooooo long for the dateeeeeeeeeeeeeeeeeeeee.'
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)
SUBSTRING(..., 3, 8000)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Exactly the way I did in the code I posted.
ASKER
Great