The DBMS_OUTPUT is said to be the built-in package which enables us for displaying output, debugging information, and send messages from PL/SQL blocks, subprograms, packages, triggers.
Example:
BEGIN
dbms_output.put_line (user || ' Teachers in the database:');
FOR t IN (SELECT table_name FROM user_teachers)
LOOP
dbms_output.put_line(t.table_name);
END LOOP;
END;
/
Datetime Data types and Functions
Given below are the Datetime data types:
1.DATE
This data type stores date and time information in both of the character and number datatypes. This is made of information like on century, year, month, date, hour, and second.
2.TIMESTAMP
This is the extension data type of the DATE. This stores year, month, and day of the DATE datatype, including with hour, minute and second values. This is very useful in storing precise time values.
3.YIMESTAMP WITH TIME ZONE
This is the variant of TIMESTAMP that includes a time zone region name or the offset in the value. Offset is the difference between the local time and UTC that is in hours and minutes as well. It is useful for collecting and then evaluating data information in all the geographic regions.
4.TIMESTAMP WITH LOCAL TIME ZONE
This is also a variant of time zone that involves a time zone offset in its value.
Below are the Datetime functions:
- ADD_MONTHS(x, y); – this adds y months to x.
- LAST_DAY(x); – this returns the last day of the month.
- MONTHS_BETWEEN(x, y); – this gives us number of months between x and y.
- NEXT_DAY(x, day); - this returns the datetime of the next day after the x.
- NEW_TIME; - this returns the time/day value that is specified by the programmer.
- ROUND(c [, unit]); - it rounds the given c.
- SYSDATE(); - this returns the current system date.
- TRUNC(x [, unit]); - this datatype truncates x.
Below are the timestamp functions.
- CURRENT_TIMESTAMP(); - this returns a TIMESTAMP WITH TIME ZONE that contains current session time, along with the time zone.
- EXTRACT({ YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | { TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | } TIMEZONE_ABBR ) FROM x) – this extracts and also returns as a result a year, month, day, month, second, or time zone from x.
- FROM_TZ(x, time_zone); - this converts TIMESTAMP x and the time zone that are specified by time_zone to a TIMESTAMP WITH TIMEZONE
- LOCALTIMESTAMP(); - This gives us TIMESTAMP that contains local time in the session time zone.
- SYSTIMESTAMP(); - this returns TIMESTAMP WITH TIME ZONE that contains current database time.
- SYS_EXTRACT_UTC(x); - this converts the TIMESTAMP WITH TIMEZONE x to a TIMESTAMP that contains date the date and time in UTC.
- TO_TIMESTAMP(x, [format]); - this converts string x into the TIMESTAMP.
- TO_TIMESTAMP_TZ(x, [format]); - this converts string x into the TIMESTAMP WITH TIME ZONE.
The Interval Data Types and Functions
Given below are the interval data types:
1.INTERVAL YEAR TO MONTH
2.INTERVAL DAY TO SECOND
INTERVAL FUNCTIONS:
This stores a period of time using the YEAR and MONTH datetime fields.
- NUMTODSINTERVAL(x, interval_unit); - this converts the number x to an INTERVAL DAY TO SECOND.
- NUMTOYMINTERVAL(x, interval_unit); - this converts the number x to an INTERVAL YEAR TO MONTH.
- TO_DSINTERVAL(x); - this converts the string to an INTERVAL DAY TO SECOND.
- TO_YMINTERVAL(x); - this converts the string to an INTERVAL YEAR TO MONTH.
Examples:
SELECT SYSDATE FROM DUAL;
Output:
09/30/2021 7:12:34
SELECT LOCALTIMESTAMP FROM DUAL;
Output:
09/30/2021 7:12:54.336000
DBMS_OUTPUT Subprograms
- DBMS_OUTPUT.DISABLE; - This method disables the message output.
- DBMS_OUTPUT.ENABLE(buffer_size IN INTEGER DEFAULT 2000); - This method enables the message output.
- DBMS_OUTPUT.GET_LINE(line OUT VARCHAR2, status OUT INTEGER) - This method retrievesa single line of buffered information.
- DBMS_OUTPUT.NEW_LINE; - This method puts an end-of-line marker.
- DBMS_OUTPUT.PUT(item IN VARCHAR2); - This method places partial line in the buffer.
- DBMS_OUTPUT.PUT_LINE(item IN VARCHAR2) – This method places a line in the buffer.
Example:
DECLARE
lines dbms_output.chararr;
num_lines number;
BEGIN
-- enable the buffer with default size 20000
dbms_output.enable;
dbms_output.put_line('Hello welcome to great learning!');
dbms_output.put_line('Hope you have enjoyed the tutorials!');
dbms_output.put_line('Have a happy learning');
num_lines := 3;
dbms_output.get_lines(lines, num_lines);
FOR i IN 1..num_lines LOOP
dbms_output.put_line(lines(i));
END LOOP;
END;
/
Output:
Hello welcome to great learning!
Hope you have enjoyed the tutorials!
Have a happy learning!
PL/SQL procedure successfully completed