SQL, where 里面有 Like 的案例

SQL, Case with Like inside of the where

有人可以帮助我吗? 我有一个具有这种结构的字段:2,3,4,5,6,0,1 他们是星期几,如果今天是一周的最后一天,我必须找到什么时候,问题是:

例如,今天是第 3 天,通过以下查询,我可以 select 所有以数字 3 结尾的查询,但如果我有一个具有此结构的人:3、0、1 或 3 ,1 或 3,0 我不明白。

declare @today int
set @today=DATEPART(dw,GETDATE())-2
print @today
select cast (cfv.value as VARCHAR)
from CompanyFieldvalues cfv
join CompanyFields cf on cf.companyFieldId=cfv.companyFieldId
where cf.name='NextDeliveryDates' and cfv.companyId in(
select cfv.companyId
from CompanyFieldvalues cfv
join Companies c on c.companyId=cfv.companyId
where cfv.value='Retailer' and c.status=1)
and (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' + cast (@today as VARCHAR))

要解决这个问题,我很难介绍一个案例,所以我的查询将是这个:

declare @today int
set @today=DATEPART(dw,GETDATE())-2
print @today
select cast (cfv.value as VARCHAR)
from CompanyFieldvalues cfv
join CompanyFields cf on cf.companyFieldId=cfv.companyFieldId
where cf.name='NextDeliveryDates' and cfv.companyId in(
select cfv.companyId
from CompanyFieldvalues cfv
join Companies c on c.companyId=cfv.companyId
where cfv.value='Retailer' and c.status=1)
and (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' + cast (@today as VARCHAR))  and  case
        when (@today != 0 or @today!=1) 
        then (cfv.value not like '%,0' or cfv.value not like '%,1')

但在这种情况下,我收到此错误消息:

Msg 156, Level 15, State 1, Line 14 Incorrect syntax near the keyword 'not'.

我确定我犯了一些愚蠢的错误,但我不明白:S

请多多帮助! 非常感谢

您可以将 CASE 语句替换为逻辑表达式:

(((@today != 0 or @today!=1) AND (cfv.value not like '%,0' or cfv.value not like '%,1')) OR NOT (@today != 0 or @today!=1))

因此您的查询将变为:

declare @today int
set @today=DATEPART(dw,GETDATE())-2
print @today
select cast (cfv.value as VARCHAR) from CompanyFieldvalues cfv
    join CompanyFields cf on cf.companyFieldId=cfv.companyFieldId
    where cf.name='NextDeliveryDates' and cfv.companyId in(
        select cfv.companyId
            from CompanyFieldvalues cfv
            join Companies c on c.companyId=cfv.companyId
            where cfv.value='Retailer' and c.status=1) 
            and (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value     like '%' + cast (@today as VARCHAR)) 
            and  (((@today != 0 or @today!=1) AND (cfv.value not like '%,0' or cfv.value not like '%,1')) OR NOT (@today != 0 or @today!=1))

使用 CASE 语句应该如下所示:

declare @today int
set @today=DATEPART(dw,GETDATE())-2
print @today

select cfv.value
from CompanyFieldvalues cfv
join CompanyFields cf on cf.companyFieldId=cfv.companyFieldId
where cf.name='NextDeliveryDates' and cfv.companyId in(
select cfv.companyId
from CompanyFieldvalues cfv
join Companies c on c.companyId=cfv.companyId
where cfv.value='Retailer' and c.status=1)

and

CASE
    WHEN (@today !=0 or @today!=1) THEN
        (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' + cast    (@today as VARCHAR) 
        or cfv.value like '%' + cast (@today as VARCHAR)+',0'
        or cfv.value like '%' + cast (@today as VARCHAR)+',0,1'
        or cfv.value like '%' + cast (@today as VARCHAR)+',1')
    ELSE
        (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' +  cast  (@today as VARCHAR))
END = 1

或 ****************************************

 CASE
    WHEN ((@today !=0 or @today!=1) AND (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' + cast    (@today as VARCHAR) 
        or cfv.value like '%' + cast (@today as VARCHAR)+',0'
        or cfv.value like '%' + cast (@today as VARCHAR)+',0,1'
        or cfv.value like '%' + cast (@today as VARCHAR)+',1'))
    THEN 1
    WHEN (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' +  cast  (@today as VARCHAR)) THEN 1
    ELSE 0
END = 1

但表达式 (@today !=0 或 @today!=1) 始终为真。也许你应该找到另一个有意义的条件

帕特里夏的更新:

这是我的 table,它只有一行带有这个值,代表一个人的最后交货日期,问题是他们有这样的结构:2,3,4,5,6 ,0,1

3,0,1
0,1
3,0
5,6
5,5
5
3

我需要知道这个人是否有今天的最后交货日(0 和 1 每次都在最后但实际上他们不是最后交货日而是第一和第二)。

我想我必须为 0 和 1 创建 2 个单独的案例,但我没有得到它们 :S 这是我实际拥有的:

declare @today int
set @today=DATEPART(dw,GETDATE())-2 /*-2 because the date are managed from a c# code so I need in this way to have the day in the format Monday=0, etc*/
print @today
select cfv.value
from CompanyFieldvalues cfv
join CompanyFields cf on cf.companyFieldId=cfv.companyFieldId
where cf.name='NextDeliveryDates' and cfv.companyId in(
select cfv.companyId
from CompanyFieldvalues cfv
join Companies c on c.companyId=cfv.companyId
where cfv.value='Retailer' and c.status=1)
and
 CASE
    WHEN ((@today !=0 or @today!=1) AND (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' + cast    (@today as VARCHAR) 
    or cfv.value like '%' + cast (@today as VARCHAR)+',0'
    or cfv.value like '%' + cast (@today as VARCHAR)+',0,1'
    or cfv.value like '%' + cast (@today as VARCHAR)+',1'))
    then 1
    WHEN (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' +  cast  (@today as VARCHAR)) THEN 1
    ELSE 0
END = 1

就像你说的那样,在第 0 天或第 1 天,我每次都会遇到问题。 如果是第 0 天,我必须 select 只是值 =0,当第 1 天是什么时候,当值=1 或值=0,1 时我必须 select 所以我想我需要修改第二个条件:

WHEN (cfv.value like '%,' + cast (@today as VARCHAR) or cfv.value like '%' +  cast  (@today as VARCHAR)) THEN 1

并创建 2 个不同的,不是吗?

如果我是你并且我很清楚你打算做什么,我会按如下方式实施。我会定义一个函数来拆分你的字符串:

create function dbo.SplitString 
(
    @str nvarchar(max), 
    @separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
    select 
        cast(1 as bigint), 
        cast(1 as bigint), 
        charindex(@separator, @str)
    union all
    select
        p + 1, 
        b + 1, 
        charindex(@separator, @str, b + 1)
    from tokens
    where b > 0
)
select
    p-1 ItemIndex,
    substring(
        @str, 
        a, 
        case 
        when b > 0 then b-a 
        ELSE LEN(@str) end) 
    AS Item
from tokens
);

GO

然后我会使用以下条件:

AND where not exists(select * from dbo.SplitString(cfv.value, ',') where    CAST(Item AS INT) > @today )   

我测试了你给我的样本,它有效。所以对于以下输入:

5 
5 
3,4
5 
5 
5 
3,0,1 
3,0 
5 
3

它returns:

3
3,0,1
3,0