SQL - 选择的星期几问题

时间:2016-05-11 19:00:14

标签: sql datetime sql-like week-number weekday

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

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

这给我一个像这样的表: 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因为使用此DB的系统以不同方式管理日期)然后我只选择公司信息和两个不同表格的交货日期。

我的问题是我需要选择今天最后交货日的公司....所以如果今天是第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

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

3 4 五 6

我尝试做出不同的加入,差异,但是我不会像今天那样只拥有最后交付日的公司。

你知道我该怎么办吗?或者如果您对我如何做有其他想法,请告诉我。

非常感谢, 卡罗

3 个答案:

答案 0 :(得分:0)

看到数据结构是这样的: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')
    )

这是错误:

  

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

答案 1 :(得分:0)

看起来您正试图在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

答案 2 :(得分:0)

解决方案(也许不是最好的,但它正在发挥作用;如果它不让我知道那么: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

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