您好我正在尝试使用日期检索昨天的转移行。在创建视图时。我过滤了要检索昨天值的日期,但它仍然以null值继续。
代码为WHERE (TranDate=DATEADD(d,-1,GETDATE())
任何帮助人?
答案 0 :(得分:0)
试试这个:
--DROP TABLE #PartTran;
CREATE TABLE #PartTran
(
PartNum INT
, WareHouseCode INT
, TranDate DATE
, TranQty INT
);
INSERT INTO #PartTran
( PartNum, WareHouseCode, TranDate, TranQty )
VALUES ( 1 -- PartNum - int
, 101 -- WareHouseCode - int
, GETDATE() - 1 -- TranDate - date
, 0 -- TranQty - int
),
( 1 -- PartNum - int
, 102 -- WareHouseCode - int
, GETDATE() - 2 -- TranDate - date
, 5 -- TranQty - int
),
( 2 -- PartNum - int
, 101 -- WareHouseCode - int
, GETDATE() -- TranDate - date
, 3 -- TranQty - int
),
( 4 -- PartNum - int
, 105 -- WareHouseCode - int
, GETDATE() - 1 -- TranDate - date
, 2 -- TranQty - int
),
( 6 -- PartNum - int
, 102 -- WareHouseCode - int
, GETDATE() - 5 -- TranDate - date
, 0 -- TranQty - int
),
( 1 -- PartNum - int
, 101 -- WareHouseCode - int
, GETDATE() -- TranDate - date
, 1 -- TranQty - int
);
SELECT PartNum
, WareHouseCode
, TranDate
, TranQty
FROM #PartTran
WHERE TranDate = DATEADD(DAY, -1, CAST(GETDATE() AS DATE));