SteveMott Posted January 19, 2023 Report Share Posted January 19, 2023 Hi I am trying to retrieve a value from a table based on the condition that it exists. CASE WHEN (SELECT COUNT(DrugReactionID) FROM FullReactionIngredients WHERE DrugReactionID = '[@calcfield:3]') = 1 THEN SELECT (Reactionname) FROM FullReactionIngredients WHERE DrugReactionID = '[@calcfield:3]' ELSE 'this is false' END Can't get this to validate. Any advice would be greatly appreciated. Thanks Steve Quote Link to comment Share on other sites More sharing options...
1 CoopperBackpack Posted January 19, 2023 Report Share Posted January 19, 2023 Hello @SteveMott, Please wrap the SELECT statement in parenthesis and test the formula. So, the formula should be: CASE WHEN (SELECT COUNT(DrugReactionID) FROM FullReactionIngredients WHERE DrugReactionID = '[@calcfield:3]') = 1 THEN (SELECT (Reactionname) FROM FullReactionIngredients WHERE DrugReactionID = '[@calcfield:3]') ELSE 'this is false' END Ilyrian 1 Quote Link to comment Share on other sites More sharing options...
0 SteveMott Posted January 19, 2023 Author Report Share Posted January 19, 2023 Once again, I can't thank you enough! Cheers Steve Quote Link to comment Share on other sites More sharing options...
0 NiceDuck Posted January 20, 2023 Report Share Posted January 20, 2023 okay, I think I misread the problem, but sharing this anyway: isnull((CAST((nullif((Select count(Reactionname) from FullReactionIngredients where DrugReactionID = '[@calcfield:3]'),1)) as VARCHAR)), (Select Reactionnamefrom FullReactionIngredients where DrugReactionID = '[@calcfield:2]')) It kinda works without but without the ELSE part, so yeah, using a conditional statement is still more preferable. Quote Link to comment Share on other sites More sharing options...
0 CoopperBackpack Posted January 20, 2023 Report Share Posted January 20, 2023 Hi @NiceDuck, actually, it is possible to use a different formula that provides the same result in this particular case. Since the COALESCE() function is a kind of syntactic shortcut for the CASE expression, but with its own nuances, the formula can be: COALESCE( (SELECT (Reactionname) FROM FullReactionIngredients WHERE DrugReactionID = '[@calcfield:3]'), 'this is false' ) The readability of such a formula is worse but is it shorter. NiceDuck 1 Quote Link to comment Share on other sites More sharing options...
Question
SteveMott
Hi
I am trying to retrieve a value from a table based on the condition that it exists.
CASE
WHEN (SELECT COUNT(DrugReactionID) FROM FullReactionIngredients WHERE DrugReactionID = '[@calcfield:3]') = 1
THEN
SELECT (Reactionname)
FROM FullReactionIngredients
WHERE DrugReactionID = '[@calcfield:3]'
ELSE 'this is false'
END
Can't get this to validate. Any advice would be greatly appreciated.
Thanks
Steve
Link to comment
Share on other sites
4 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.