日期:2014-05-17 浏览次数:20618 次
declare @dtmStart date ='2013-05-25 10:0:01:035'
declare @dtmFinish date ='2013-06-07 10:0:01:035'
;
with tb as(
select a=@dtmStart
union all
select DATEADD(day,1,a) from tb where a<@dtmFinish
)
select a=stuff((select ','+substring(convert(varchar,a),10,1)
from tb for xml path('')),1,1,'')
CREATE FUNCTION sf_GetDateLastNum
(
@BDate DATETIME,
@EDate DATETIME
)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @RetVal VARCHAR(100)
DECLARE @TBL TABLE(IID INT IDENTITY(1,1), LastDay VARCHAR(1))
SET @RetVal = ''
INSERT INTO @TBL(LastDay)
SELECT RIGHT(DAY(DATEADD(DD, NUMBER, @BDate)), 1)
FROM MASTER..SPT_VALUES
WHERE TYPE='p' AND DATEADD(DD, NUMBER, @BDate)<=@EDate
DELETE @TBL WHERE IID NOT IN (SELECT MIN(IID) FROM @Tbl GROUP BY LastDay)
SELECT @RetVal = @RetVal + LastDay + ',' FROM @TBL
RETURN LEFT(@RetVal, LEN(@RetVal) - 1)
END
GO
SELECT dbo.sf_GetDateLastNum('2013-05-25 10:0:01:035', '2013-06-07 10:0:01:035')
/*
5,6,7,8,9,0,1,2,3,4
*/