我有一个日期字段为lastDelivery的表,我想检索所有比今天的日期早6个月或更长的记录,我该怎么做?
答案 0 :(得分:8)
试试这个:
SELECT * FROM Table
WHERE lastdelivery <= dateadd(month, -6, getdate())
答案 1 :(得分:1)
DECLARE @subscriptions TABLE
(
OrderId int,
OrderName varchar(255),
lastDelivery date default getdate()
)
INSERT INTO @subscriptions (OrderName, lastDelivery)
VALUES
( 'Allen', '2015-05-20'),
( 'Bob', '2015-06-20'),
( 'Craig', '2015-07-20'),
( 'David', '2015-08-20'),
( 'Edward', '2015-09-20'),
( 'Frank', '2015-10-20'),
( 'George', '2015-11-20'),
( 'Harry', '2015-12-20')
SELECT OrderName FROM @subscriptions
WHERE lastDelivery <= DATEADD(MONTH, -6, GETDATE())
答案 2 :(得分:1)
使用DATEADD
<强>查询强>
select * from your_table_name
where lastDelivery <= dateadd(month, -6, getdate());
答案 3 :(得分:0)
我尝试了上述方法中的某些方法,但它们对我不起作用,但这可行:
SELECT * FROM `table_Columns` WHERE `date` <= '2020-01-13 04:40:51'
请注意,我的日期已手动设置为6个月前。