0% found this document useful (0 votes)
4 views

Assignment 4

The document provides an overview of SQL functions and commands related to date and time formatting, including the TO_CHAR function, INTERVAL clause, and NEXT_DAY function. It includes examples of SQL queries for displaying formatted dates, creating tables, and inserting data. Additionally, it demonstrates how to query employee and train data based on various date and time conditions.

Uploaded by

GARIMA SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 4

The document provides an overview of SQL functions and commands related to date and time formatting, including the TO_CHAR function, INTERVAL clause, and NEXT_DAY function. It includes examples of SQL queries for displaying formatted dates, creating tables, and inserting data. Additionally, it demonstrates how to query employee and train data based on various date and time conditions.

Uploaded by

GARIMA SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Format Model Description Example Output

DD Day of the month (2 digits) 28


MON Abbreviated month name JAN
MONTH Full month name JANUARY
YYYY 4-digit year 2025
`HH:MI AM Hour and minute with AM/PM 02:30 PM
DAY Full name of the day TUESDAY

The TO_CHAR function in DBMS is used to convert dates, numbers, or other data types into
a formatted string.

TO_CHAR(value, format_model)

value: The date, number, or other data type you want to convert.

format_model: The format in which you want the output.

SELECT TO_CHAR(SYSDATE, 'dd-MM-yy') AS formatted_date FROM DUAL;


Interval Clause: The INTERVAL keyword in SQL is used to specify a period of time (duration)
that can be added to or subtracted from a date or timestamp. It is typically used with date
and time arithmetic, such as adding or subtracting hours, minutes, days, months, or years to
a date or timestamp.

INTERVAL 'value' unit

value: This is the numerical value of the time period you want to add or subtract (e.g., 1, 30,
etc.).

unit: This specifies the time unit for the duration (e.g., DAY, HOUR, MINUTE, SECOND,
MONTH, YEAR).

SELECT CURRENT_DATE + INTERVAL '3' MONTH;

SELECT CURRENT_DATE + INTERVAL '5' DAY;

SELECT CURRENT_TIMESTAMP - INTERVAL '30' MINUTE;


The NEXT_DAY function in Oracle SQL is used to find the next occurrence of a specific day of
the week after a given date.

-- 1. Display the system date

SELECT SYSDATE AS "System Date" FROM DUAL;

-- 2. Display the current day

SELECT TO_CHAR(SYSDATE, 'DAY') AS "Current Day" FROM DUAL;

-- 3. Display the current month and spell out the year

SELECT TO_CHAR(SYSDATE, 'MONTH YYYY') AS "Current Month and Year" FROM DUAL;

-- 4. Display the current date spelled out

SELECT TO_CHAR(SYSDATE, 'DD-MONTH-YYYY') AS "Spelled Out Date" FROM DUAL;

-- 5. Check whether it is AM or PM right now

SELECT TO_CHAR(SYSDATE, 'AM') AS "AM or PM" FROM DUAL;

-- 6. Display the date of next Friday

SELECT NEXT_DAY(SYSDATE, 'FRIDAY') AS "Next Friday Date" FROM DUAL;


-- 7. Round the system date on month

SELECT ROUND(SYSDATE, 'MONTH') AS "Rounded Date (Month)" FROM DUAL;

-- 8. Truncate the system date on month

SELECT TRUNC(SYSDATE, 'MONTH') AS "Truncated Date (Month)" FROM DUAL;

-- 9. Round the system date on year

SELECT ROUND(SYSDATE, 'YEAR') AS "Rounded Date (Year)" FROM DUAL;

-- 10. Truncate the system date on year

SELECT TRUNC(SYSDATE, 'YEAR') AS "Truncated Date (Year)" FROM DUAL;

-- 11. Find the day after three days

SELECT SYSDATE + 3 AS "Day After Three Days" FROM DUAL;

-- 12. Create an EMP table

CREATE TABLE EMP (

Empno NUMBER PRIMARY KEY,

Name VARCHAR2(20),

Date_of_Joining DATE

);

-- Insert sample data into the EMP table

INSERT INTO EMP (Empno, Name, Date_of_Joining)

VALUES (101, 'Alice', TO_DATE('2025-01-01', 'YYYY-MM-DD'));

INSERT INTO EMP (Empno, Name, Date_of_Joining)


VALUES (102, 'Bob', TO_DATE('2025-01-08', 'YYYY-MM-DD'));

INSERT INTO EMP (Empno, Name, Date_of_Joining)

VALUES (103, 'Charlie', TO_DATE('2025-01-15', 'YYYY-MM-DD'));

INSERT INTO EMP (Empno, Name, Date_of_Joining)

VALUES (104, 'Diana', TO_DATE('2024-12-20', 'YYYY-MM-DD'));

INSERT INTO EMP (Empno, Name, Date_of_Joining)

VALUES (105, 'Eve', TO_DATE('2025-01-22', 'YYYY-MM-DD'));

COMMIT;

-- 13. Display the day of the Date_of_Joining column

SELECT Empno, Name, TO_CHAR(Date_of_Joining, 'DAY') AS "Day of Joining"

FROM EMP;

-- 14. Display those employees who join the company on Monday

SELECT Empno, Name, Date_of_Joining

FROM EMP

WHERE TO_CHAR(Date_of_Joining, 'DAY') = 'MONDAY';

-- 15. Display those employees who join the company this month

SELECT Empno, Name, Date_of_Joining

FROM EMP WHERE TO_CHAR(Date_of_Joining, 'MM-YYYY') = TO_CHAR(SYSDATE, 'MM-


YYYY');

-- 16. Display those employees who joined the company in the last 30 days
SELECT Empno, Name, Date_of_Joining FROM EMP WHERE Date_of_Joining BETWEEN
SYSDATE - 30 AND SYSDATE;

-- 17. Create a table Train

CREATE TABLE Train (

TrainNo NUMBER(6) PRIMARY KEY,

Date_of_Departure DATE,

Time_of_Departure TIMESTAMP,

Time_of_Arrival TIMESTAMP

);

-- 18. Insert five records into the table Train

INSERT INTO Train (TrainNo, Date_of_Departure, Time_of_Departure, Time_of_Arrival)

VALUES (101001, TO_DATE('2025-01-28', 'YYYY-MM-DD'), TO_TIMESTAMP('2025-01-28


09:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2025-01-28 13:30:00', 'YYYY-MM-
DD HH24:MI:SS'));

INSERT INTO Train (TrainNo, Date_of_Departure, Time_of_Departure, Time_of_Arrival)

VALUES (102002, TO_DATE('2025-01-28', 'YYYY-MM-DD'), TO_TIMESTAMP('2025-01-28


15:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2025-01-28 20:00:00', 'YYYY-MM-
DD HH24:MI:SS'));

INSERT INTO Train (TrainNo, Date_of_Departure, Time_of_Departure, Time_of_Arrival)

VALUES (103003, TO_DATE('2025-01-29', 'YYYY-MM-DD'), TO_TIMESTAMP('2025-01-29


10:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2025-01-29 16:45:00', 'YYYY-MM-
DD HH24:MI:SS'));

INSERT INTO Train (TrainNo, Date_of_Departure, Time_of_Departure, Time_of_Arrival)

VALUES (104004, TO_DATE('2025-01-28', 'YYYY-MM-DD'), TO_TIMESTAMP('2025-01-28


22:30:00', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2025-01-29 02:00:00', 'YYYY-MM-
DD HH24:MI:SS'));
INSERT INTO Train (TrainNo, Date_of_Departure, Time_of_Departure, Time_of_Arrival)

VALUES (105005, TO_DATE('2025-01-30', 'YYYY-MM-DD'), TO_TIMESTAMP('2025-01-30


05:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2025-01-30 11:00:00', 'YYYY-MM-
DD HH24:MI:SS'));

COMMIT;

-- 19. Display all the records

SELECT * FROM Train;

-- 20. Display the time values inserted in the columns

SELECT TrainNo, TO_CHAR(Time_of_Departure, 'HH:MI:SS AM') AS "Time of Departure",

TO_CHAR(Time_of_Arrival, 'HH:MI:SS AM') AS "Time of Arrival"

FROM Train;

-- 21. Display those trains which arrived in PM

SELECT TrainNo, TO_CHAR(Time_of_Arrival, 'HH:MI:SS AM') AS "Arrival Time"

FROM Train

WHERE TO_CHAR(Time_of_Arrival, 'AM') = 'PM';

-- 22. Display train numbers departing in the next one hour

SELECT TrainNo, Time_of_Departure

FROM Train

WHERE Time_of_Departure BETWEEN SYSTIMESTAMP AND SYSTIMESTAMP + INTERVAL '1'


HOUR;

You might also like