We are migrating the data from SQL Server to Teradata and I am unable to replicate the same code from SQL Server to Teradata.
Description: I want to generate report between the dates received from the Rstart and Rend, which is basically 6 month duration (1. jan to jun & 2. jul to dec ). For this, I need to store the dates in two variables (Rstart,Rend) and inorder to fetch records between those two dates
SQL Server code
DECLARE @REnd Date;
DECLARE @RStart Date;
SET @REnd = (SELECT MAX(CalendarDate) AS @REnd
FROM Table1
WHERE CalendarDate = MonthEndDate
AND Monthofyear IN ('June', 'December')
AND CalendarDate < CURRENT_DATE());
SET @RStart = (SELECT MAX(CalendarDate) AS @RStart
FROM Table1
WHERE CalendarDate = (CalendarDate - Extract(Day From CalendarDate) + 1)
AND Monthofyear IN ('January', 'July')
AND CalendarDate < @REnd);
SELECT *
FROM Table2
WHERE ReviewDate BETWEEN @REnd AND @RStart;
This is what I have tried in Teradata:
Create Procedure Demo()
Begin
Declare REnd Date;
Declare RStart Date;
Set REnd = (select max(CalendarDate) as REnd
from Table1
where CalendarDate = MonthEndDate
and Monthofyear in ('June', 'December')
and CalendarDate < Current_date()
);
Set RStart = (select max(CalendarDate) as RStart
from Table1
where CalendarDate = (CalendarDate - Extract(Day From CalendarDate)+1)
and Monthofyear in ('January', 'July')
and CalendarDate < REnd
);
Call dbc.sysexecsql(('select * from table 2 where reviewdate between' ||REnd|| 'and' ||RStart|| ');');
End;