We help IT Professionals succeed at work.

A pl/sql program to display date variable and assigns sysdate to it?

rockvault
rockvault asked
on
Medium Priority
1,952 Views
Last Modified: 2007-12-19
I need to write a pl/sql program that declares a date variable named 'today' and assigns sysdate to it.  Output should hopefully look like the following:
(day 1-10) "It is the <day number> of <month name>.  It is early in the month,"

Here is what I have so far...

>declare
todaysdate date;
positionofday binary_integer;
today varchar2(9);
begin
todaysdate := sysdate;
positionofday := instr(today, 'day');
today :=to_char(sysdate, 'day');
  dbms output.put_line(today is '|| today || to_char(todaysdate));
if positionofday = '1' then
  dbms_output.put_line('it is early in the month');
elsif positionofday = '2' then
  dbms_output.put_line('it is early in the month');
elsif positionofday = '3' then
  dbms_output.put_line('it is early in the month');
elsif positionofday = '4' then
  dbms_output.put_line('it is early in the month');
elsif positionofday = '5' then
  dbms_output.put_line('it is early in the month');
elsif positionofday = '6' then
  dbms_output.put_line('it is early in the month');
else
  dbms_output.put_line('error displaying day information');
end if;
end;
/

THanks for any and all help.
-rockvault
Comment
Watch Question

Commented:
How about something like:

declare
  v_today_str  VARCHAR2(60);
  v_day        NUMBER;
begin
  v_today_str := 'It is day '||to_char(sysdate, 'DD')||' of '||to_char(sysdate, 'MONTH')||'. ';
  v_day := to_number(to_char(sysdate, 'DD'));
  IF ((v_day/10) <= 1)
  THEN
    v_today_str := v_today_str || 'It is early in the month.';
  ELSIF ((v_day/20) <= 1)
  THEN
    v_today_str := v_today_str || 'It is the middle of the month.';
  ELSE
    v_today_str := v_today_str || 'It is late in the month.';
  END IF;
 
  dbms_output.put_line(v_today_str);
 
end;
Commented:
maybe this....

DECLARE
todayno       NUMBER  (2,0);
monthname     VARCHAR2(20);
positionofday VARCHAR2(30);
earlymonth    VARCHAR2(30) := '.  It is early in the month.';
midmonth      VARCHAR2(30) := '.  It is mid month.';
latemonth     VARCHAR2(30) := '.  It is late in the month.';

BEGIN
todayno       := TO_NUMBER(TO_CHAR(sysdate, 'dd'));
monthname     := TO_CHAR(sysdate,'Month');

IF todayno < 11 THEN
   positionofday := earlymonth;
ELSIF todayno BETWEEN 11 AND 19 THEN
      positionofday := midmonth;
ELSE  positionofday := latemonth;
END IF;
DBMS_OUTPUT.PUT_LINE ('It is the ' || todayno || ' day of ' || monthname  || positionofday);  
END;
/
KongAWS Certified Solutions Architect - Professional
CERTIFIED EXPERT

Commented:
Hi all, would you settle for a basic SQL statement?

select 'It is the ' || to_char(sysdate, 'DD') ||
       ' of the month. It is ' ||
       case when to_char(sysdate, 'DD') < 10
          then ' early '
          else ' late ' end
       || ' in the month.' "Today's Date"
from dual;

Today's Date
------------------------------------------------------
It is the 25 of the month. It is  late  in the month.

BTW, I hope you've got 8i...


Author

Commented:
mozartny,
thanks a lot, just what i needed.
-rockvault

Explore More ContentExplore courses, solutions, and other research materials related to this topic.