October 20, 2022 at 2:13 pm
Hi,
I am trying to put a case statement in a where clause and nothing that I am doing seems to work, that I got online.
Basically, I need to put in the where clause that if the state = MN then these ProcCodes are Excluded but in any other state then they and others go.
Here is my where clause:
WHERE MyState = 'MN' THEN ProcCode NOT IN ('Kl', 'Kjh') ELSE PrcCode ('kl', 'Kjh', 'bn') END
AND PayDt > '12/01/20'
Thnak you
October 20, 2022 at 2:28 pm
Hi,
I am trying to put a case statement in a where clause and nothing that I am doing seems to work, that I got online.
Basically, I need to put in the where clause that if the state = MN then these ProcCodes are Excluded but in any other state then they and others go.
Here is my where clause:
WHERE MyState = 'MN' THEN ProcCode NOT IN ('Kl', 'Kjh') ELSE PrcCode ('kl', 'Kjh', 'bn') END
AND PayDt > '12/01/20'Thnak you
You're missing the CASE
in your CASE expression.
The main problem is that you are trying to return a Boolean value from your CASE expression, but CASE will never return a Boolean value. This is because SQL Server does not recognize a Boolean data type.
You need to return a specific (set of) value(s) and then test the returned value. Something like
WHERE CASE WHEN MyState <> 'MN' THEN 1
WHEN ProcCode NOT IN ('Kl', 'Kjh') THEN 1
ELSE 0
END = 1
AND PayDt > '12/01/20'
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
October 20, 2022 at 2:33 pm
Thanks for the reply, but that is not what I need.
I need a way to write this that when the state is MN then these ProcCodes are Excluded, but in any other state then they are good.
Do you know how AI can write that?
Thank you
October 20, 2022 at 2:49 pm
Thanks for the reply, but that is not what I need.
I need a way to write this that when the state is MN then these ProcCodes are Excluded, but in any other state then they are good.
Do you know how AI can write that?
Thank you
I don't think you need a CASE statement, you just need an OR.
WHERE (State = 'MN' AND ProdCodes IN ('Kl', 'Kjh') )
OR State <> 'MN'
October 20, 2022 at 3:08 pm
That may work but I need something a little different:
I need the if it is State = MN then Exclude ProdCodes IN ('Kl', 'Kjh') Else if State not = MN then allow them.
But I do not know how to write this.
Thank you
October 20, 2022 at 3:14 pm
That may work but I need something a little different:
I need the if it is State = MN then Exclude ProdCodes IN ('Kl', 'Kjh') Else if State not = MN then allow them.
But I do not know how to write this.
Thank you
WHERE (State = 'MN'
AND ProdCodes NOT IN ('Kl', 'Kjh') )
OR State <> 'MN'
October 20, 2022 at 3:23 pm
This code would check for State = 'MN'
AND ProdCodes NOT IN ('Kl', 'Kjh') )
I need it to exclude ProdCodes NOT IN ('Kl', 'Kjh') When the state is MN.
October 20, 2022 at 3:33 pm
Please post sample data and expected outcome.
Please follow the guidance here
October 20, 2022 at 3:46 pm
IF OBJECT_ID('tempdb..#temp','u') is not null
DROP TABLE #temp
GO
SELECT *
INTO #temp
FROM (VALUES ('MN', 'KL'),
('MN', 'Kjh'),
('MN', 'XYZ'),
('XX', 'KL'),
('XX', 'Kjh'),
('XX', 'ABC')) T(State, ProdCodes)
GO
SELECT *
FROM #temp
GO
SELECT *
FROM #temp
WHERE (State = 'MN' AND ProdCodes IN ('Kl', 'Kjh') )
OR State <> 'MN'
GO
SELECT *
FROM #temp
WHERE (State = 'MN'
AND ProdCodes NOT IN ('Kl', 'Kjh') )
OR State <> 'MN'
October 20, 2022 at 4:35 pm
okay, this works.
Thanks for the help
October 20, 2022 at 7:24 pm
Thanks for the reply, but that is not what I need.
I need a way to write this that when the state is MN then these ProcCodes are Excluded, but in any other state then they are good.
Do you know how AI can write that?
Thank you
Your responses show that you are not bothering to read the suggestions and figure out how they work. My response does EXACTLY what you asked for, it's just evaluated in a slightly different order.
WHEN MyState <> 'MN' THEN 1
-- "in any other state then they are good"
WHEN ProcCode NOT IN ('Kl', 'Kjh') THEN 1
-- we know MyState = 'MN' in order to get to this point and this includes every ProcCode except the ones you want to exclude.
ELSE 0
-- we know that MyState = 'MN' and ProcCode in ('Kl', 'Kjh') to get to this point, and "these ProcCodes are Excluded"
You had a very similar response to Jonathan AC Roberts.
If you expect us to put in the work to help you, then you need to demonstrate that you are also willing to put in the work, and you clearly are showing that you're not willing to put in the work.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
January 10, 2024 at 6:55 am
This was removed by the editor as SPAM
January 10, 2024 at 7:00 am
This was removed by the editor as SPAM
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply