我正在努力解决这个问题。
如果是星期一到星期四,我想提前90天;
SELECT
PR.ClientID
,PR.NewDealEndDate
INTO
#OriginalRenewalDetails
FROM
Shiva.dbo.ProductRemortgage PR WITH (NOLOCK)
LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK)
ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
WHERE -- 90 days from now.
PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
AND CL.ClientID IS NULL
但是,如果是星期日,我希望在Where子句中更改以下内容。所以我也得到周五和周六的结果。
WHERE -- 90 days from now.
PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE())) AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
AND CL.ClientID IS NULL
我找不到一种有效的方法。任何帮助表示赞赏。
答案 0 :(得分:2)
使用IF
。
伪:
if day = sunday
begin
select with 88
end
else
begin
select with 90
end
或者将WHERE子句更改为:
WHERE (PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
AND CL.ClientID IS NULL AND Datepart(weekday, getdate()) <> 1)
OR
(PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE()))
AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
AND CL.ClientID IS NULL
AND Datepart(weekday, getdate()) = 1)
答案 1 :(得分:1)
您可以执行以下操作:
SELECT PR.ClientID
, PR.NewDealEndDate
INTO #OriginalRenewalDetails
FROM Shiva.dbo.ProductRemortgage PR WITH (NOLOCK)
LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK)
ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
WHERE CL.ClientID IS NULL
AND
(
(
PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
AND Datepart(weekday, getdate()) <> 1 -- 1 is Sunday
)
OR
(
PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE()))
AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
AND Datepart(dayofweek, getdate()) = 1
)
)
答案 2 :(得分:1)
declare @adjustment int--SELECT DATEPART(weekday, getdate())
set @adjustment =
CASE
WHEN DATEPART(weekday, getdate())
in (1,2,3,4,5) THEN 90
ELSE 88
END
WHERE
PR.NewDealEndDate BETWEEN DATEADD(dd, @adjustment...
答案 3 :(得分:1)
此处不需要动态SQL或两个单独的查询。使用您当前使用的任何逻辑替换**DAY = SUNDAY**
部分以确定日期是星期日:
WHERE PR.NewDealEndDate BETWEEN DATEADD(DAY,
CASE WHEN **DAY = SUNDAY** THEN 88 ELSE 90 END,
DATEDIFF(DAY, 0, GETDATE())) AND DATEADD(DAY, 90, DATEDIFF(dd, 0, GETDATE()))
AND CL.ClientID IS NULL;
您也可以事先计算范围,例如
DECLARE @start SMALLDATETIME, @end SMALLDATETIME;
SET @end = DATEADD(DAY, 90, DATEDIFF(DAY, 0, GETDATE()));
SET @start = CASE WHEN **DAY = SUNDAY** THEN DATEADD(DAY, -2, @end) ELSE @end END;
WHERE PR.NewDealEndDate BETWEEN @start END @end
AND CL.ClientID IS NULL;