SQL Server takes all the dates or all the months in a certain time period

Source: Internet
Author: User

Take all months:

DECLARE @begin datetime, @end datetime set @begin = ' 2015-2-6 ' Set @end = ' 2015-12-2 ' declare @months int set @months =datediff ( Month, @begin, @end) Select CONVERT (varchar (7), DATEADD (Month,number, @begin), as month from Master.dbo.spt_values where Type= ' P ' and number<[email protected]

Take all dates

DECLARE @days INT,  @date_start datetime = ' 2017-03-15 ',  @date_end datetime = ' 2017-04-13 '   SET @days = datedif F (Day, @DATE_START, @DATE_END);    SELECT convert (char), DATEADD (DD, number, @DATE_START), as YYYYMMDD from    master.dbo.spt_values as  Sptwhere   type = ' P ' and number  <= @days

You can then do some left join operations with a table to get a date that has not occurred for some time in a table. A small function to use to record a bit.

SQL Server takes all the dates or all the months in a certain time period

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.