SQL Server gets the consecutive year, month, day

Source: Internet
Author: User

if exists(Select 1  fromsysobjectswhereType='TF'  andName='atpublic_tf_getsummarydate')    Drop functionatpublic_tf_getsummarydateGoCreate functionAtpublic_tf_getsummarydate (@calltype varchar(1)="',--0: Year 1: Month 1: Day    @startdateDATE,--Start Date    @enddateDATE--End Date)  /** * author:create date:2018-01-18 Description: Loop return Date Example: * **/RETURNS @retDateValue TABLE(DateValuevarchar( +))                                                                                as    begin    if @calltype='0'    begin        Insert  into @retDateValue(datevalue)Select substring(Convert(varchar( +),DATEADD( Year, Number,@startdate), +),1,4) asYearvalue fromMaster.. Spt_valuesWHERETYPE= 'P'  and DATEADD( Year, Number,@startdate)<= @enddate                    End    Else if @calltype='1'    begin        Insert  into @retDateValue(datevalue)Select substring(Convert(varchar( +),DATEADD(MONTH, Number,@startdate), +),1,7) asYearvalue fromMaster.. Spt_valuesWHERETYPE= 'P'  and DATEADD(MONTH, Number,@startdate)< @enddate                    End    Else if @calltype='2'    begin        Insert  into @retDateValue(datevalue)Select Convert(varchar( +),DATEADD( Day, Number,@startdate), +)+'00:00:00'  asYearvalue fromMaster.. Spt_valuesWHERETYPE= 'P'  and DATEADD( Day, Number,@startdate)< @enddate                    End    returnEnd                                                                        Go

Invocation Example:

Declare @beginDate varchar( +)="',@endDate varchar( +)="',@datebefore datetimeSelect @beginDate='2017-01-01',@endDate='2017-05-31'Select @datebefore=CONVERT(datetime,@beginDate)SelectDateValue fromAtpublic_tf_getsummarydate ('0',@beginDate,@endDate)SelectDateValue fromAtpublic_tf_getsummarydate ('1',@beginDate,DateAdd( Year,1,@datebefore))SelectDateValue fromAtpublic_tf_getsummarydate ('1',@beginDate,DateAdd(MONTH,1,@datebefore))SelectDateValue fromAtpublic_tf_getsummarydate ('2',@beginDate,DateAdd(MONTH,1,@datebefore))

SQL Server gets the consecutive year, month, day

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.