sqlserver 获取到日期范围内每天,每周,每月,每年记录

--每天记录
DECLARE
@start DATE,@end DATE SET @start='2020-7-1' SET @end='2020-7-20' select DATEADD(DAY,number,@start) ymd from master..spt_values where type='p' AND number<=DATEDIFF(day,@start,@end)

 

--每周记录
DECLARE @start DATE,@end DATE
SET @start='2020-6-1'
SET @end='2020-7-1'

select 
DATEADD(WEEK,number,@start) ymd
from master..spt_values
where type='p'  AND 
number<=DATEDIFF(WEEK,@start,@end)

 

 

--每月记录
DECLARE
@start DATE,@end DATE SET @start='2019-1-1' SET @end='2020-7-1' select DATEADD(MONTH,number,@start) ymd from master..spt_values where type='p' AND number<=DATEDIFF(MONTH,@start,@end)

 

 

--每年记录
DECLARE
@start DATE,@end DATE SET @start='2019-1-1' SET @end='2020-7-1' select DATEADD(year,number,@start) ymd from master..spt_values where type='p' AND number<=DATEDIFF(year,@start,@end)

 

posted @ 2020-07-06 17:35  Lin_pin  阅读(1460)  评论(0编辑  收藏  举报