SQL Queries for Date and Train Management
SQL Queries for Date and Train Management
First, create the table using: CREATE TABLE Train (Train_Number INTEGER PRIMARY KEY, Date_Of_Departure DATE, Time_Of_Departure TIMESTAMP, Time_Of_Arrival TIMESTAMP). Then, format timestamps using TO_TIMESTAMP function. For example, to insert a record: INSERT INTO Train VALUES (101, TO_DATE('2024-10-16', 'YYYY-MM-DD'), TO_TIMESTAMP('2024-10-16 10:30:00', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2024-10-16 16:00:00', 'YYYY-MM-DD HH24:MI:SS')).
You can identify trains arriving in the PM by using the TO_CHAR function on the Time_Of_Arrival with the 'AM' check. The query is: SELECT * FROM Train WHERE TO_CHAR(Time_Of_Arrival, 'AM') = 'PM';
To display employees who joined on a Monday, you would use the TO_CHAR function on the Date_Of_Joining to match 'Monday'. The query is: SELECT * FROM Employee WHERE TO_CHAR(Date_Of_Joining, 'Day') = 'Monday';
To find the next occurrence of a specific weekday, such as Friday, you use the NEXT_DAY function. The query is: SELECT NEXT_DAY(SYSDATE, 'FRIDAY') AS Next_Friday FROM DUAL;
To round the current system date to the nearest year, you can use the ROUND function with the 'YEAR' argument. The query is: SELECT ROUND(SYSDATE, 'YEAR') AS Rounded_Year FROM DUAL;
To identify a train departing within the next hour, use the BETWEEN clause with SYSTIMESTAMP and an interval. The query is: SELECT Train_Number FROM Train WHERE Time_Of_Departure BETWEEN SYSTIMESTAMP AND SYSTIMESTAMP + INTERVAL '1' HOUR;
To display the spelled-out current date in SQL using Oracle Database, you can use the TO_CHAR function with the appropriate format. The query is: SELECT TO_CHAR(SYSDATE, 'DDth Month, YYYY') AS Spell_Out_Date FROM DUAL;
To list employees who joined in the last 30 days, compare Date_Of_Joining with SYSDATE minus 30 days. The query is: SELECT * FROM Employee WHERE Date_Of_Joining >= SYSDATE - 30;
Use the TO_CHAR function to extract the AM/PM indicator from SYSDATE. The query is: SELECT TO_CHAR(SYSDATE, 'AM') AS AM_PM FROM DUAL;
To truncate the current system date to the beginning of the month, you can use the TRUNC function. The query is: SELECT TRUNC(SYSDATE, 'MONTH') AS Truncated_Date FROM DUAL;