Oracle SQL provides a rich set of built-in functions to manipulate date/time values, perform string operations, and convert data between different data types (such as strings to numbers, strings to dates, and dates to formatted strings).
Purpose: Stores date and time (year, month, day, hour, minute, second) in a fixed 7-byte internal format.
Important: Display format by default is DD-MON-YY (e.g., 13-NOV-92). Time defaults to 00:00:00 (midnight) if not specified.
format_mask: Optional, but highly recommended. Describes how string1 is structured (e.g., 'DD-MON-YYYY', 'YYYY/MM/DD'). If omitted, Oracle uses the default session format, which can cause errors (ORA-01843).
Example:
SELECT TO_DATE('28-04-2026', 'DD-MM-YYYY') FROM dual;
Purpose: A dummy table in Oracle used when a SELECT statement must have a FROM clause but no actual table data is needed.
Usage: Used for evaluating expressions, functions, or calculations. It always contains exactly one row and one column.
Purpose: Returns the date of the first instance of a specified day of the week that occurs after the given date. Always moves forward, never returns the same day.
Requirement: "Display all employees who were hired in January (any year) from the EMPLOYEE table."
Step 1: Identify the table → EMPLOYEEStep 2: Identify the column → HIREDATE (Date type)
Step 3: Extract the month component → Since we need to filter by month, we must convert the internal DATE to a string representing the month. Use TO_CHAR with format 'MON' or 'MM'.
Step 4: Write the filter condition → TO_CHAR(HIREDATE, 'MON') = 'JAN' (or 'MM' = '01').
Step 5: Write query:
SELECT * FROM EMPLOYEE WHERE TO_CHAR(HIREDATE, 'MON') = 'JAN';
Step 6: Validate → Check if the result set only contains employees hired in January.
Requirement: List employee names and their hire date in the format "NAME joined on 17th December Nineteen Eighty".
Query:
SELECT ENAME || ' joined on ' || TO_CHAR(HIREDATE, 'DDth Month YYYY') AS Join_DateFROM EMP;
Note: DDth adds "th" (e.g., 17th, 2nd, 3rd depending on Oracle's TH format element, which might need SP or TH modifications). An even more professional approach for the specific text requirement: TO_CHAR(HIREDATE, 'DDth "of" Month "Nineteen" YYYY') - depending on the exact requirement.
Data Warehousing: Professionals use TO_DATE with explicit format masks to safely load data from flat files into Oracle databases, ensuring date integrity regardless of the source format.
Reporting Dashboards: TO_CHAR is heavily used to format financial figures (e.g., '$9,999,999.00') and dates (e.g., 'MM/DD/YYYY') for UI components.
Temporal Analytics: MONTHS_BETWEEN and ADD_MONTHS are essential for calculating loan interest, subscription renewal dates, and employee tenure.
Legacy Data Migration: When migrating data from older systems, TO_DATE with custom masks handles varied date formats (e.g., 'yyyy-dd-mm').
Always use explicit format masks with TO_DATE and TO_CHAR to avoid ambiguity and dependency on NLS (National Language Support) settings.
Prefer the || concatenation operator over CONCAT, as it is more readable and supports multiple strings directly without nesting.
Use COALESCE over NVL because COALESCE is ANSI standard and can handle more than two arguments, making it more flexible.
Index on dates: Consider function-based indexes (e.g., CREATE INDEX idx_emp_yr ON EMP(TO_CHAR(HIREDATE, 'YYYY'))) if you frequently filter by formatted year in large tables.
An e-commerce site stores HOME_PHONE, MOBILE_PHONE, and WORK_PHONE. Write a query to select the CUSTOMER_NAME and the first available phone number, preferring MOBILE_PHONE if HOME_PHONE is NULL, and WORK_PHONE if both previous are NULL.
Explain the difference between TO_DATE and TO_CHAR.
TO_DATE converts a string to a DATE object (for storage/filtering). TO_CHAR converts a date or number to a formatted string (for display/reporting).
Why is it critical to provide a format mask with TO_DATE?
To avoid ambiguity and dependency on session NLS settings. Without a mask, Oracle relies on the default NLS_DATE_FORMAT, which varies by environment and can cause runtime errors (ORA-01843) or incorrect date interpretation.
How does COALESCE handle NULLs?
It scans the arguments from left to right and returns the first expression that is NOT NULL. If all expressions are NULL, it returns NULL.
What does ADD_MONTHS return if the input date is the last day of a month and the target month has fewer days?
It returns the last day of the target month. For example, ADD_MONTHS('28-FEB-2026', 1) returns 31-MAR-2026 (if 2026 is leap year, it would handle differently), but generally, Oracle adjusts to the last valid day of the target month.
Explain how to find employees hired in a specific decade (e.g., the 1980s).
Use TO_CHAR(HIREDATE, 'YYYY') BETWEEN '1980' AND '1989'.
Mastering these functions transforms a basic SQL user into a professional developer. You can now handle complex date ranges, format output for business reports, and safely convert user inputs. Always test date logic thoroughly, as time zone and NLS settings can produce unexpected results. Practice the lab tasks below to solidify your skills.
Note: The words 'NINETEEN' and 'EIGHTY' are hardcoded text representing the year in words. In a real scenario, you would use TO_CHAR(HIREDATE, 'YYYY SP') for a fully written year, but the lab specifically asks for "NINETEEN EIGHTY" for a specific date.
B. ALI SEVENTEENTH DEC NINETEEN EIGHTY
SELECT ENAME || ' ' || TO_CHAR(HIREDATE, 'DDth MON "NINETEEN" YYYY') FROM EMP; -- Similar, but note the exact spelling.
C. ALI week day of joining
SELECT ENAME, TO_CHAR(HIREDATE, 'DAY') AS WEEKDAY FROM EMP;
D. ALI 17/12/80
SELECT ENAME, TO_CHAR(HIREDATE, 'DD/MM/YY') AS FORMATTED_DATE FROM EMP;