SQL,Case with Like里面的地方

时间:2016-05-12 09:51:40

标签: sql database datetime case ssms-2012

有人可以帮我吗? 我有一个这种结构的字段:2,3,4,5,6,0,1 它们是一周中的某一天,如果今天是本周的最后一个交付日,我必须找到问题是:

示例,今天是第3天,通过以下查询,我可以选择所有以数字3结束但是如果我有某个具有此结构的人:3,0,1或3,1或3,0我不懂。

ng-transclude

要修复它我很难介绍一个案例,所以我的查询将是这个:

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))

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

  

消息156,级别15,状态1,行14关键字附近的语法不正确   '不'。

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

有些帮助! 非常感谢

3 个答案:

答案 0 :(得分:0)

您可以将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))

答案 1 :(得分:0)

使用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)始终为真。也许你应该找到另一个有意义的条件

答案 2 :(得分:0)

如果我是你,我理解你打算做什么,我会按照以下方式实施。我会定义一个分割你的字符串的函数:

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

它返回:

3
3,0,1
3,0