日期:2014-05-18  浏览次数:20613 次

大侠们,怎么拆分时间段?
SQL code

--怎么拆分时间段?
create table #tb(a datetime,b datetime)
insert into #tb values('2011-01-15 10:15:00','2011-01-17 09:25:00')

/*
--拆成
'2011-01-15 10:15:00'        '2011-01-15 23:59:59'
'2011-01-16 00:00:00'        '2011-01-16 23:59:59'
'2011-01-17 00:00:00'        '2011-01-17 09:25:00'
*/



不好意思,分数不够了。。

------解决方案--------------------
SQL code
create table #tb(a datetime,b datetime)
insert into #tb values('2011-01-15 10:15:00','2011-01-17 09:25:00')


SELECT 
CASE WHEN a.number=0 THEN b.a+a.number ELSE CONVERT(VARCHAR(10),b.a+a.number,120) END AS StartDate,
CASE WHEN DATEDIFF(dd,b.a+a.number,b)=0 THEN b.b ELSE  CONVERT(VARCHAR(10),b.a+a.number,120)+' 23:59:59' END AS EndDate
FROM master.dbo.spt_values  AS a ,#tb AS b 
WHERE 
DATEDIFF(dd,b.a+a.number,b)>=0
and a.type='P' AND a.number>=0

/*
2011-01-15 10:15:00.000    2011-01-15 23:59:59.000
2011-01-16 00:00:00.000    2011-01-16 23:59:59.000
2011-01-17 00:00:00.000    2011-01-17 09:25:00.000
*/