I have created a function which need input as month(11) and year(2018) and it will return the first Sunday of the next month.
Create or replace function get_effectivedate (par_Month in int,Par_Year in int);
Return date
Is
startDate varchar;
edate date;
begin
startDate := par_Month+Par_Year;
edate := select next_day(add_months(trunc(startDate, 'MM'), 1), 'Sunday') as EffectiveDate from dual;
return edate;
end;
This function is prompting me while running and it is throwing some errors on compilation.
Error:
LINE/COL ERROR
--------- -------------------------------------------------------------
7/1 PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: := . ( @ % ; not null range with default character The symbol ";" was substituted for "BEGIN" to continue.
9/14 PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null <an identifier> <a double-quoted delimited-identifier> <a bind variable> continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an alternat
11/2 PLS-00103: Encountered the symbol "RETURN"
Errors: check compiler log
Function GET_EFFECTIVEDATE compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
7/1 PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: := . ( @ % ; not null range with default character The symbol ";" was substituted for "BEGIN" to continue.
9/14 PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null <an identifier> <a double-quoted delimited-identifier> <a bind variable> continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an alternat
11/2 PLS-00103: Encountered the symbol "RETURN"
Errors: check compiler log
Function GET_EFFECTIVEDATE compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
7/1 PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: := . ( @ % ; not null range with default character The symbol ";" was substituted for "BEGIN" to continue.
9/14 PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null <an identifier> <a double-quoted delimited-identifier> <a bind variable> continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an alternat
11/2 PLS-00103: Encountered the symbol "RETURN"
Errors: check compiler log
You want to call startDate variable so remove ampersand sign,
&
is s a sign for prompting user's input. Change your line in code to:You have several issues:
create ...
line;&
) - which you are being prompted for;int
instead ofnumber
for the argument types (not really an error, but using native types is usuually better);startdate
local variable is declared asvarchar
instead ofvarchar2
, which works but is discouraged, but either way you have to specify a laximum length for the string (i.e.startdate varchar2(6);
);+
, and putting the result of that (i.e. 2029) into a string variable;&
- as a number, not a date, but thentrunc
won't work;trunc
is pointless anyway.You can do this quite simply as:
then call it as:
or for all months this year via a CTE:
Converting both of the arguments to strings explicitly lets you control leading zeros, so you can be sure that passing in say 1 and 234 will convert to strings '
01'
and'0234'
, so when they are concatenated they form'023401'
, to match the format model.If you don't specify a day number explicitly then
to_date()
gives you the first day of the month.The function above gets the second Sunday if the first of the month is itself a Sunday, which you said in comments is what you actually want. (Notice 2018-07-08). If you did actually just want the first Sunday with no further adjustment you do either:
or more simply:
either of which givesL