表如下:
表A
barcode inserttime
1234567 2009-08-08
7654321 2009-08-09
5678901 2009-08-10
表B 注:周期是维护周期,既每间隔多少天维护一次
barcode zhouqi
1234567 1
7654321 2
5678901 3
现在求: 表A的条码(barcode),在加上指定的维护日期后与当前时间对比,如果超过了当前日期,读取出来
既:
1234567 需要把他的inseertime 2009-08-08加上他的zhouqi :1然后与当前时间对比getdate()小弟暂时写出如下:
select * from wupin where DateAdd(dd,3,lasttime)>getdate(), 主要中间的那个3天?1天?2天,怎么确认相加?
表A
barcode inserttime
1234567 2009-08-08
7654321 2009-08-09
5678901 2009-08-10
表B 注:周期是维护周期,既每间隔多少天维护一次
barcode zhouqi
1234567 1
7654321 2
5678901 3
现在求: 表A的条码(barcode),在加上指定的维护日期后与当前时间对比,如果超过了当前日期,读取出来
既:
1234567 需要把他的inseertime 2009-08-08加上他的zhouqi :1然后与当前时间对比getdate()小弟暂时写出如下:
select * from wupin where DateAdd(dd,3,lasttime)>getdate(), 主要中间的那个3天?1天?2天,怎么确认相加?
解决方案 »
- substr问题
- 如何去除字段identity属性
- 已建发布与订阅,可是数据没有同步,要手动“生成快照”数据才能同步,Help Me!TKS!
- 这样的一条select 语句,mssql是怎么处理的?
- 存储过程中如果表存在则继续执行后面的,否则先创建再执行,请教写法
- 有没有其他方法?
- sql2005远程数据导入
- 请问使用触发器的时候,能否使特定字段的更新不触发触发器
- word打印sqlserver数据,能做得到吗?
- 问题一:如何用作业实现SQL的增量自动备份?问题二:如何将SQL备份到其他机器上?
- 请问这样的查询问题怎么解决?
- ---输入两个日期,如何用表生成不间断的日期(年、月、日)递增记录行?--
修改了你的数据-- =============================================
-- Author: T.O.P
-- Create date: 2009/11/28
-- Version: SQL SERVER 2005
-- =============================================
declare @TB1 table([barcode] int,[inserttime] datetime)
insert @TB1
select 1234567,'2009-11-26' union all
select 7654321,'2009-11-27' union all
select 5678901,'2009-11-28'declare @TB2 table([barcode] int,[zhouqi] int)
insert @TB2
select 1234567,1 union all
select 7654321,2 union all
select 5678901,3SELECT A.*
FROM @TB1 A INNER JOIN @TB2 B ON A.BARCODE = B.BARCODE
WHERE DATEADD(DD,ZHOUQI,[inserttime])>GETDATE()--测试结果:
/*
barcode inserttime
----------- -----------------------
7654321 2009-11-27 00:00:00.000
5678901 2009-11-28 00:00:00.000(2 row(s) affected)
*/
create table #A(barcode int, inserttime datetime);
insert into #A
select 1234567 , '2009-08-08 ' union all
select 7654321 , '2009-08-09 ' union all
select 5678901 , '2009-11-26'create table #B(barcode int, zhouqi int);
insert into #B
select 1234567 , 1 union all
select 7654321 , 2 union all
select 5678901 , 3
select #A.* from #A join #B on #A.barcode = #B.barcode
where datediff(day, dateadd(day, #B.zhouqi, #A.inserttime), getdate()) < 0/*
结果:
barcode inserttime
----------- -----------------------
5678901 2009-11-26 00:00:00.000(1 行受影响)
*/