34,587
社区成员
发帖
与我相关
我的任务
分享
create table tb(id char(3),品名 nvarchar(20),批号 char(6),效期 char(7))
insert into tb select '001', '阿莫西林' , '070707', '2009-08'
insert into tb select '002' , '土霉素片' , '060505' , '2008-05'
insert into tb select '003' , '养阴清肺膏', '060606' , '2008-06'
insert into tb select '004' , '益母草膏' , '070101' , '2009-01'
select *
from tb
where datediff(month,getdate(),convert(char(10),效期+'-1',120)) between 0 and 6
drop table tb
/*
id 品名 批号 效期
---- -------------------- ------ -------
002 土霉素片 060505 2008-05
003 养阴清肺膏 060606 2008-06
(所影响的行数为 2 行)
*/
SELECT * FROM
[TYourTable]
WHERE DATEDIFF(MONTH,[效期],GETDATE())<=6)--只查6个月的则为 =
select * from tab where datediff(month,getdate(),效期)<=6
select * from 表
where 有效期 > getdate() and datediff(mm, getdate(), 有效期) = 6