比较2个日期和条件

时间:2016-03-11 08:42:46

标签: sql sql-server tsql

我正在尝试将两个日期与付款条件进行比较:

  1. 日期1:发票日期
  2. 日期2:付款日期
  3. 付款条件(天)
  4. 我想计算付款是否在付款条件之内或之内完成。到目前为止我尝试了什么,但没有预期的结果。

    SELECT
    (
    CASE 
        WHEN  ISNUMERIC(a.paymentcondition) >=  DATEDIFF(day,a.payment_date,a.invoice_date) 
        THEN 'yes' 
        ELSE 'no' 
    END) 
    AS 'within_payment_condition'
    FROM finance
    

1 个答案:

答案 0 :(得分:0)

假设您有一个名为test1的表格如下:

create table test1 (id int, date1 date, date2 date);
insert into test1 values (1, '2015-04-06', '2015-04-05'), (2, '2015-06-06', '2015-06-07');

然后您可以使用以下查询比较日期:

select *, 

case 
when test1.date1 < test1.date2 then 'test1 is smaller' 
else 'test1 is not smaller' 
end as comment

from test1

结果将如下:

enter image description here

相关问题