日期:2014-05-17 浏览次数:20391 次
CREATE TABLE #temp(id INT, beginDate DATETIME, endDate DATETIME)
INSERT #temp SELECT 1, '2013-08-01', '2013-08-04'
SELECT
a.id,
[Date] = CONVERT(CHAR(10), DATEADD(DAY, number, beginDate), 120)
FROM #temp a
CROSS APPLY
(
SELECT TOP(DATEDIFF(DAY, A.beginDate, A.endDate)+1) number
FROM MASTER..spt_values
WHERE type ='p'
ORDER BY number
) b
/*
id Date
1 2013-08-01
1 2013-08-02
1 2013-08-03
1 2013-08-04
*/
select
a.id,convert(varchar(10),dateadd(d,b.number,a.beginDate),120) as Date
from 你的表名 a
left join master..spt_values b on b.type='p'
where dateadd(d,b.number,a.beginDate)<=a.[endDate]
SELECT COUNT(*) FROM master..spt_values b WHERE b.TYPE='p'
--2048
declare @t TABLE
([id] int,[beginDate] datetime,[endDate] datetime)
insert @t
select 1,'1900-01-01','2013-08-06'
select
a.id,
CONVERT(VARCHAR(10),DATEADD(d,b.number,a.beginDate),120) AS Date
from @t a
LEFT JOIN master..spt_values b ON 1=1 AND b.TYPE='p'
WHERE DATEADD(d,b.number,a.beginDate)<=a.[endDate]
--此例最多只能找到 1905-08-10