34,588
社区成员
发帖
与我相关
我的任务
分享
if object_id('FFFF') is not null drop table FFFF
create table FFFF
(
tdate datetime
)
declare @i int
set @i=(select datediff(dd,'2009-1-25','2009-1-28'))
select dateadd(dd,1,'2009-1-25')
while @i>0
begin
insert into FFFF select dateadd(dd,@i-1,'2009-1-25')
set @i=@i-1
if(@i=1)
begin
insert into FFFF select '2009-1-28'
end
end
select * from FFFF
create table FFFF
(
tdate datetime
)
declare @i int
set @i=(select datediff(dd,'2009-1-25','2009-1-28'))
select dateadd(dd,1,'2009-1-25')
while @i>0
begin
insert into FFFF select dateadd(dd,@i-1,'2009-1-25')
set @i=@i-1
end
select * from FFFF
select rn = identity(int, 0, 1) into #t from sysobjects;
select m.BeginDate + t.rn from #t t, mytab m where m.BeginDate + t.rn <= m.EndDate;
drop table #t;
select a.n+10*b.n as n
from (select 0 as n union all select 1 union all select 2
union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8 union all select 9 ) a
,(select 0 as n union all select 1 union all select 2
union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8 union all select 9 ) b
declare @t table(BeginDate datetime,EndDate datetime)
insert into @t select '2009-1-25','2009-1-28'
select
dateadd(dd,b.id,a.BeginDate) as date
from
@t a,
(select (select count(1) from sysobjects where id<t.id) as id from sysobjects t) b
where
b.id<=datediff(dd,BeginDate,EndDate)
/*
date
------------------------------------------------------
2009-01-25 00:00:00.000
2009-01-26 00:00:00.000
2009-01-27 00:00:00.000
2009-01-28 00:00:00.000
*/
declare @t table(BeginDate datetime,EndDate datetime)
insert into @t select '2009-1-25','2009-1-28'
select
dateadd(dd,b.id,a.BeginDate) as date
from
@t a,
(select (select count(1) from sysobjects where id<t.id) as id from sysobjects t) b
where
b.id<=datediff(dd,BeginDate,EndDate)
/*
date
------------------------------------------------------
2009-01-25 00:00:00.000
2009-01-26 00:00:00.000
2009-01-27 00:00:00.000
2009-01-28 00:00:00.000
*/
------------------------------------
-- Author: happyflsytone
-- Date:2008-11-17 17:32:45
------------------------------------
-- Test Data: TA
IF OBJECT_ID('TA') IS NOT NULL
DROP TABLE TA
Go
CREATE TABLE TA(BeginDate SMALLDATETIME,EndDate SMALLDATETIME)
Go
INSERT INTO TA
SELECT '2009-1-25','2009-1-28'
GO
--Start
SELECT
dateadd(d,n, BeginDate) as d
FROM
TA a,
(select 0 as n union all select 1 union select 2 union all select 3 union all select 4) b
where dateadd(d,n, BeginDate) <= EndDate
--Result:
/*
d
-----------------------
2009-01-25 00:00:00
2009-01-26 00:00:00
2009-01-27 00:00:00
2009-01-28 00:00:00
(4 行受影响)
*/
--End