I have a table with 3 fields userID, state, country and I am trying to run a query that finds all records where the country is Great Brittin, Canada or the US and if the country is US I only want records in California, Utah and Arizona.
I thought this might work but no
select *
from myTable
where county in ('GB','CA','US')
and case when country = 'US' then state in ('CA','UT','CA') end
Any SQL gurus have an idea?
**edit**
I think I tried to over simplify this. The countries and state are passed as variables so not sure if there will be a ‘US’ country but if there is there will be a state list
declare @country varchar(500)
, @state varchar(500)
, @sqlStr varchar(4000)
select @sqlStr = 'select * from Addresses
WHERE country IN ('+@country+')
OR (country = ''US'' AND stateCode IN ('+@state+'))'
exec (@sqlStr)
which makes this harder.
replace the last line with something like that: