- declare @date datetime
- set @date='2009-02-05'
- select [day]=convert(varchar,DATEADD(mm,DATEDIFF(mm,0,@date),0)+number,121)
- from master..spt_values
- where type='p'
- and number< datediff(dd,@date,dateadd(mm,1,@date))<pre name="code" class="sql">--输出2008年至今以来的月份列表:
- create table Mon
- (
- ID int identity(1,1),
- Mon varchar(6)
- )
- GO
- DECLARE
- @BeginMonth varchar(6),
- @EndMonth varchar(6)
- SELECT
- @BeginMonth='200801',
- @EndMonth='200906'
- INSERT Mon(Mon)
- SELECT
- CONVERT(VARCHAR(6),DATEADD(month,number,@BeginMonth+'01'),112)
- FROM
- master..spt_values
- WHERE
- type='P'
- and
- DATEADD(month,number,@BeginMonth+'01')<=@EndMonth+'01'
- SELECT * FROM Mon
- DROP TABLE Mon
利用master..spt_values来生成连续日期
最新推荐文章于 2024-01-19 10:08:08 发布