简体   繁体   English

CASE GETDATE() - SQL Server 中的 GETDATE()

[英]CASE GETDATE() - GETDATE() in sql server

can anyone explain the below example?谁能解释下面的例子?

Query询问

SELECT (CASE WHEN (GETDATE() - GETDATE()) < 31 THEN  1 ELSE 0 END) [Result],
(CASE WHEN (GETDATE() - '2020-08-30') < 31 THEN  1 ELSE 0 END) [Result1],
(CASE WHEN (GETDATE() - '2020-07-30') < 31 THEN  1 ELSE 0 END) [Result2],
(CASE WHEN (GETDATE() - '2020-07-30') < 31 THEN  1 ELSE 0 END) [Result3]

Output输出

Result  Result1 Result2 Result3
1        1      0       0

I want to know just way Result and Result1 has 1 and else 0 ?我想知道ResultResult11还是0

Actual Query实际查询

SELECT  ((CASE WHEN (GETDate()-[InvBillDate])<31 then 1 
else 
(case when (GETDate()-[InvBillDate])<61 And (GETDate()-[InvBillDate])>30 then 2 
else 
(case when (GETDate()-[InvBillDate])<91 And (GETDate()-[InvBillDate])>60 then 3 
else 4 end) 
end) end)) AS [ColNo] 
FROM [dbo].[Invoice] (NOLOCK)

If you don't mind, I suggest this way to shorten a bit the query.如果你不介意,我建议用这种方式来缩短查询。

SELECT (
       (CASE WHEN (GETDate()-[InvBillDate])<31 then 1 
       else 
       (case when (GETDate()-[InvBillDate])<61 then 2 
       else 
       (case when (GETDate()-[InvBillDate])<91 then 3 
       else 4 
       end) end) end)
       ) AS [ColNo] 
FROM [dbo].[Invoice] (NOLOCK)

With limited context, I will try to interpret the query.在有限的上下文中,我将尝试解释查询。

It is trying to calculate the date difference between today and the billing date, then based on that categorize the date differences into 4 groups:它试图计算今天和计费日期之间的日期差异,然后基于此将日期差异分为 4 组:

  • Within 31 days 31天内
  • 31 - 60 days 31 - 60 天
  • 61 - 90 days 61 - 90 天
  • More than 90 days超过 90 天

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

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