SQL Server - 带有DATEDIFF函数的WHERE子句中的CASE WHEN

时间:2018-05-02 03:38:33

标签: sql-server where-clause datediff case-when

我正在尝试使用case和DATEDIFF函数执行下面的where语句。但我得到语法错误。请帮帮我。

WHERE
    -- primary contract is Flowing
    CTS.ContractStatusIdentifier = 'FLW'                                         
    AND (CASE 
            WHEN VMO.ProvinceOrStateCode = 'ON'
               THEN DATEDIFF (dd, CTR.RenewalDate, @currentdate) >= 75
               ELSE DATEDIFF (dd, CTR.RenewalDate, @currentdate) >= 45)
    AND VMO.ProvinceOrStateCode IN ('ON','AB')

2 个答案:

答案 0 :(得分:1)

显然是AND (CASE WHEN VMO.ProvinceOrStateCode = 'ON' THEN DATEDIFF (dd, CTR.RenewalDate, @currentdate) >= 75 ELSE DATEDIFF (dd, CTR.RenewalDate, @currentdate) >= 45) 子句语法

AND DATEDIFF (dd, CTR.RenewalDate, @currentdate)
    >= (CASE 
           WHEN VMO.ProvinceOrStateCode = 'ON'
               THEN 75
               ELSE 45
        END)

无效。为简单起见非常简单,请尝试

const listForHeader = sections.filter((section, i) => {
    return section === "top" || section === "bottom";
});

const listForArticle = sections.filter((section, i) => {
    return section != "top" || section != "bottom";
})
祝你好运

答案 1 :(得分:0)

SELECT [supp_call_tckt_num],UPPER([assigned_to])as assigned_to,

assigned_date,close_date,diffday = CASE WHEN,close_date为null然后

datediff(d,assigned_date,getdate())else

datediff(d,assigned_date,close_date)结束,

UPPER([call_source])as call_source,

UPPER([account_type])为account_type,[mdn],

UPPER([status])作为status,UPPER([company_name])作为company_name,

UPPER([rep_id])为rep_id,UPPER([caller_first_name])caller_first_name,

UPPER([caller_last_name])为caller_last_name,[account_number],

UPPER([network])为网络,UPPER([issue_type])为issue_type,

UPPER([问题])为问题,UPPER([subtype])为子类型,UPPER([prob_desc_res])

作为prob_desc_res,UPPER([notes])作为注释,[ticket_number] FROM [ewts_tracker]与(NOLOCK)WHERE [ewts_tracker]。[escalated] =

'是'和 datediff(d,isnull(assigned_date,getdate()),close_date)> =' 3'