日期:2014-05-18 浏览次数:20509 次
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]([typeName] varchar(6),[theDate] datetime) insert [test] select '哈密瓜','2012-03-01' union all select '雪莲','2012-03-08' union all select '西瓜','2012-03-13' select [typeName],[theDate] from( select *, DATEADD(mm,3,[theDate]) as enddate from test)t where enddate>='2012-05-26' and [theDate] between '2012-04-27' and '2012-05-26'
------解决方案--------------------
你给的条件中,其实5月26号就根本没有用了,只要保质期大于4月27号就都属于这个范围内的。。。
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba') BEGIN DROP TABLE tba END GO CREATE TABLE tba ( typeName VARCHAR(100), theDate VARCHAR(100) ) INSERT INTO tba SELECT '哈密瓜', '2012-03-01' UNION SELECT '雪莲', '2012-03-08' UNION SELECT '西瓜', '2012-03-13' UNION SELECT '美人瓜','2012-4-25' --4.27-5.26 SELECT * FROM tba WHERE DATEADD(MONTH,3,theDate) > '2012-04-27' typeName theDate 哈密瓜 2012-03-01 美人瓜 2012-4-25 西瓜 2012-03-13 雪莲 2012-03-08