显示所有 IN 运算符值,状态为 'available' 或 'not available'
Display all IN operator value, with status of 'available' or 'not available'
我有像 ('222','333','43242','3242')
这样的字符串列表,我必须在 table dummy
中使用它,假设我的 table 虚拟对象只有这个列表中的两个值('222','333')
,数据应该显示为
dummyID dummyStatus
222 Available
333 Available
43242 Not Available
3242 Not Available
我已经尝试了所有可能的查询,例如
select dummyId, case when dummyId is null 'Available' else 'Not Available' end
from dummy
where dummyId in ('222','333','43242','3242')
但这行不通。
- 你
Case When ... Then End
语法不正确
- 您应该使用 所需的 table 值,如下所示
select v1.value,
case when dummyId is not null then 'Available' else 'Not Available' end as dummyStatus
from (
values('222'),('333'),('43242'),('3242')
)v1(value)
left join (
values('222'),('333') -- Let's say your table here
)dummy(dummyId) on v1.value = dummy.dummyId
已更新(参数为“(222,333,43242,3242)”)
假设我们有一个strSplit
函数来帮助我们从字符串拆分为table,那么你可以通过这种方式实现
declare @str varchar(200) = '(222,333,43242,3242)'
set @str = REPLACE(REPLACE(@str,'(', ''), ')', '')
select v1.val,
case when dummyId is not null then 'Available' else 'Not Available' end as dummyStatus
from strSplit(@str, ',')v1
left join (
values('222'),('333') -- Let's say your table here
)dummy(dummyId) on v1.val = dummy.dummyId
注意: SQL Server 2016 及更高版本 STRING_SPLIT Then You just do like this, demo on db<>fiddle
declare @str varchar(200) = '(222,333,43242,3242)'
set @str = REPLACE(REPLACE(@str,'(', ''), ')', '')
select v1.value,
case when dummyId is not null then 'Available' else 'Not Available' end as dummyStatus
from STRING_SPLIT(@str, ',')v1
left join (
values('222'),('333') -- Let's say your table here
)dummy(dummyId) on v1.value = dummy.dummyId
输出
我有像 ('222','333','43242','3242')
这样的字符串列表,我必须在 table dummy
中使用它,假设我的 table 虚拟对象只有这个列表中的两个值('222','333')
,数据应该显示为
dummyID dummyStatus
222 Available
333 Available
43242 Not Available
3242 Not Available
我已经尝试了所有可能的查询,例如
select dummyId, case when dummyId is null 'Available' else 'Not Available' end
from dummy
where dummyId in ('222','333','43242','3242')
但这行不通。
- 你
Case When ... Then End
语法不正确 - 您应该使用 所需的 table 值,如下所示
select v1.value,
case when dummyId is not null then 'Available' else 'Not Available' end as dummyStatus
from (
values('222'),('333'),('43242'),('3242')
)v1(value)
left join (
values('222'),('333') -- Let's say your table here
)dummy(dummyId) on v1.value = dummy.dummyId
已更新(参数为“(222,333,43242,3242)”)
假设我们有一个strSplit
函数来帮助我们从字符串拆分为table,那么你可以通过这种方式实现
declare @str varchar(200) = '(222,333,43242,3242)'
set @str = REPLACE(REPLACE(@str,'(', ''), ')', '')
select v1.val,
case when dummyId is not null then 'Available' else 'Not Available' end as dummyStatus
from strSplit(@str, ',')v1
left join (
values('222'),('333') -- Let's say your table here
)dummy(dummyId) on v1.val = dummy.dummyId
注意: SQL Server 2016 及更高版本 STRING_SPLIT Then You just do like this, demo on db<>fiddle
declare @str varchar(200) = '(222,333,43242,3242)'
set @str = REPLACE(REPLACE(@str,'(', ''), ')', '')
select v1.value,
case when dummyId is not null then 'Available' else 'Not Available' end as dummyStatus
from STRING_SPLIT(@str, ',')v1
left join (
values('222'),('333') -- Let's say your table here
)dummy(dummyId) on v1.value = dummy.dummyId
输出