首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何插入两个日期之间的所有日期

2012-03-25 
怎么插入两个日期之间的所有日期?Table1Store StartDate EndDateA店2011123020120101Table2Store DateA店2

怎么插入两个日期之间的所有日期?
Table1
Store StartDate EndDate 
A店 20111230 20120101

Table2
Store Date
A店 20111230
A店 20111231
A店 20120101
由Table1 生成Table2 怎么写

[解决办法]

SQL code
if not object_id('ta') is null    drop table taGoCreate table ta([Store] nvarchar(2),[StartDate] Datetime,[EndDate] Datetime)Insert taselect N'A店','20111230','20120101'UNION ALLselect N'B店','20111130','20111205'GoSELECT a.[Store],       DATEADD(DAY,b.number,a.[StartDate])[Date]FROM master..spt_values b,ta aWHERE b.type='P'AND   b.number BETWEEN 0 AND DATEDIFF(day,a.[StartDate],a.[EndDate]) /*Store Date----- -----------------------A店    2011-12-30 00:00:00.000A店    2011-12-31 00:00:00.000A店    2012-01-01 00:00:00.000B店    2011-11-30 00:00:00.000B店    2011-12-01 00:00:00.000B店    2011-12-02 00:00:00.000B店    2011-12-03 00:00:00.000B店    2011-12-04 00:00:00.000B店    2011-12-05 00:00:00.000*/
[解决办法]
SQL code
create table t1(    store varchar(10),    startdate datetime,    enddate datetime)insert into t1 select 'A店', '20111230', '20120101'select * from t1select store,DATEADD(DAY,number,a.startdate) as riqifrom t1 as a with(nolock) cross join master..spt_values as b with(nolock)where b.type='P' and DATEADD(DAY,number,a.startdate)<=a.enddate-------------------------------store    riqiA店    2011-12-30 00:00:00.000A店    2011-12-31 00:00:00.000A店    2012-01-01 00:00:00.000 

热点排行