SQL - select 的星期几问题

SQL - Day of the week issue on select

我的查询有问题.... 我有一个这样的查询:

declare @today int
set @today=DATEPART(dw,GETDATE())-2
select cast (cfv.value as VARCHAR), cfv.companyId
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) + '%' */

这给我一个 table 这样的结果: Unique Account of a company, Delivery Days

CM001 | 2,4,1
CD04 | 3,3,4
CS7 | 2
CR001 | 4
FG076 | 3,3,5,4 JUH768 | 2,2,2
HG006 | 2
KG040 | 3,2,5

简而言之,我只是在@today 中保存了一周中实际日期的值(-2,因为使用此数据库的系统以不同的方式管理日期)然后我只是 select 来自两个不同 table 的公司信息和交货日期。

我的问题是我需要 select 今天是最后交货日的公司....所以如果今天是第 2 天我可以有最后交货日 1,2 - 0 的公司, 2 - 0,1,2 等...

如果你在我的代码中看到最后一行被注释,如果你添加这一行你会得到另一个结果:

CM001 | 2,4,1
CS7 | 2
JUH768 | 2,2,2
HG006 | 2
KG040 | 3,2,5

但是通过这种方式,如您所见,我 select 不同的公司没有将当天作为最后交货日。

所以我计算了一个包含所有未来日期的动态 table:

declare @day int
set @day=DATEPART(dw,GETDATE())-1
declare @week int
set @week=7
declare @extra table
(extraday varchar)
while (@day<@week)
begin
insert into @extra (extraday) values (@day)
set @day=@day+1
end

这给了我这个结果: Days of the week future than the current one

3 4个 5个 6

我试着做不同的加入,不同,比如,但我不能像今天那样只拥有最后交货日的公司。

你知道我该如何解决吗?或者如果您对我的操作有其他想法,请告诉我。

非常感谢, 卡罗

看到数据结构是这个:2,3,4,5,6,0,1,找到部分解法是这样的:

   declare @today int
set @today=DATEPART(dw,GETDATE())-2-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))

如果当天以当天结束,则为最后一天;但我还是要例外: 例子: 4,5,0 2,1 等....

解决我很难执行 IF 但收到错误消息的问题,有人知道我该怎么做吗?

declare @today int
set @today=DATEPART(dw,GETDATE())-2-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 (
        if (@today != 0 or @today!=1)
            (cfv.value not like '%,0'  or cfv.value not like '%,1')
    )

这是错误:

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

您似乎正试图在 WHERE 子句中实施条件逻辑,但您的做法不正确。您需要将语句分解或使用动态字符串构建来创建查询并执行它。它应该看起来像这样。根据您对 @today 的验证例程,您可能需要添加一些保护措施以防止 SQL 注入。

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

declare @nsql nvarchar(max)

set @nsql=N'
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)  


if (@today != 0 or @today!=1)
set @nsql=@nsql+N'
        and ((cfv.value not like ''%,0''  or cfv.value not like ''%,1''))'

print @nsql
--exec sp_executesql @nsql

解决方案(可能不是最好的,但它正在工作;如果它不是请告诉我: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*/
declare @case as CHAR (5)
if (@today=0)(select @case='zero')
if (@today=1)(select @case='one')
if (@today>1)(select @case='other')
select cfv.value, cfv.companyId
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 ((@case='other') 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 ((@case ='zero') AND(cfv.value='0')) THEN 1
    WHEN ((@case ='one') AND(cfv.value='1' or cfv.value='0,1')) THEN 1
    ELSE 0
END = 1

非常感谢您的帮助,您的提示对我帮助很大 ;)