繁体   English   中英

SQL-选择星期几

[英]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) + '%' */

结果就是这样的表格: 公司的唯一帐户,交货天数

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,因为使用此数据库的系统以不同的方式管理日期),然后我选择公司信息和两个不同表的交货日期。

我的问题是,我只需要选择今天作为最后交货日的公司即可。...因此,如果今天是第2天,我可以让公司具有最后交货日1,2-0,2-0,1,2等。 ..

如果您在我的代码中看到最后一行被注释,则如果添加此行,则会得到其他结果:

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

但是,正如您所看到的,通过这种方式,我选择了当天没有最后交货日的其他公司。

因此,我计算了一个包含所有将来日期的动态表:

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

这给了我这样的结果: 星期几比当前的星期几

3 4 5 6

我尝试使参加者变得不同,也有所作为,但我不能像今天那样拥有最后交付日的公司。

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

非常感谢Carlo

看到数据结构就是这样的: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等...

解决我很难做中频但收到错误消息,有人知道我该怎么做吗?

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

这是错误:

消息156,级别15,状态1,第14行关键字'if'附近的语法错误。 消息102,级别15,状态1,第15行'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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM