4

我需要创建一个包含 CASE 语句的查询,该语句基本上说

CASE if getdate() > datedue by 31 days
    then status = 'Blocked
END

有谁知道如何检查今天的日期是否大于 25/10/2012 的 31 天?

编辑

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DueDate,
pm.Overdue,

HERE I NEED TO WRITE A CASE STATEMENT TO INSERT INTO A LOCKEDACCOUNT TABLE
so for example CASE WHEN DATEDIFF(dd, GETDATE(), pm.DueDate) >= 31 THEN INSERT INTO LOCKEDACCOUNT (id, status, datelocked, customerid) VALUES (.....)
END

from Customers cu
4

2 回答 2

6

像这样:

SELECT 
  CASE 
    WHEN DATEDIFF(dd, GETDATE(), @duedate) >= 1 THEN 'blocked' 
    ELSE 'Not' 
  END AS Status;

SQL 小提琴演示

请注意:如果您没有指定ELSE子句,则默认为NULL.

更新:您可以使用如下表达式插入表中CASE

INSERT INTO Statuses VALUES
(CASE 
   WHEN DATEDIFF(dd, GETDATE(), CAST('20121025' AS DATE)) >= 31 THEN 'Blocked' 
   ELSE 'Not' 
 END);

更新的 SQL Fiddle 演示

于 2012-11-21T12:23:06.313 回答
1
select CASE 
   WHEN DATEDIFF(dd,@duedate,getdate()) >= 31 then 'Blocked' else 'NO' end
于 2012-11-21T12:45:04.837 回答