Last 2 Year data with Start Date and Month

San 161 Reputation points
2021-08-18T08:44:29.74+00:00

Dear Experts,

My requirement is I need to bring the data from Src (SrcCoulmn: DataTime) for the Last 2 years based on Today's date with the start date/Month is 01-Apr-YYYY (Last year).

Eg: If Today's Date (18-Aug-2021). I need data from 01-April-2020 till Today's Date

How this can be done in Tsql and in SSIS.

Much Appricated.

Cheers
San

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,705 questions
Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-08-18T08:56:04.083+00:00

    Hi @ San-5300,

    Welcome to the microsoft TSQL Q&A forum!

    Please try:

    SELECT Src  FROM yourtable
    WHERE Src BETWEEN CAST(DATEPART(yyyy,GETDATE())-1 AS VARCHAR)+
    CAST('-04-01'AS VARCHAR) AND GETDATE()
    

    In addition, we recommend that you post questions on the TSQL and SSIS forums so that you can get more professional help.

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,516 Reputation points
    2021-08-18T08:55:10.967+00:00

    That can be done with a little "calculation" and some conversion to get the dates for filtering:

    declare @today date = getdate();
    declare @fiscalStart date;
    
    set @fiscalStart = convert(date, convert(varchar(4), year(@today) - 1) + '-04-01')
    
    select @fiscalStart, @today
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.