Oracle Date and time conversion
Here is a comprehensive guide to Oracle date and time formats, covering the fundamental data types and how to use them.
The Core Data Types
Oracle has three primary data types for storing date and time information:
- DATE: The classic data type. It stores both date and time down to the second. · Format: YYYY-MM-DD HH24:MI:SS (It always has these components, even if you don't see them). · Storage: It stores century, year, month, day, hour, minute, and second. It does not store fractional seconds or time zone information. · Example Storage: 2023-10-25 14:30:22
- TIMESTAMP: An extension of DATE that includes fractional seconds. · Format: YYYY-MM-DD HH24:MI:SS.FF · Precision: By default, FF has 6 digits of precision (microseconds). You can specify a precision from 0 to 9 (e.g., TIMESTAMP(3) for milliseconds). · Example Storage: 2023-10-25 14:30:22.123456
- TIMESTAMP WITH TIME ZONE: A TIMESTAMP that also stores the time zone region or time zone offset. · Format: YYYY-MM-DD HH24:MI:SS.FF TZR or YYYY-MM-DD HH24:MI:SS.FF TZH:TZM · Use Case: Essential for applications across different time zones. · Example Storage: 2023-10-25 14:30:22.123456 US/Pacific or 2023-10-25 14:30:22.123456 -07:00
- TIMESTAMP WITH LOCAL TIME ZONE: Similar to the above, but the time is normalized to the database's time zone for storage. When a user queries the data, it is displayed in the user's session time zone. · Use Case: Simplifies display for users in their local time, while handling conversions automatically.
- Formatting Dates and Times (TO_CHAR)
The TO_CHAR function is used to convert a DATE or TIMESTAMP into a formatted string.
Syntax:
TO_CHAR(date_column, 'format_model')
Common Format Models:
Format Element Description Example Output YYYY 4-digit year 2023 YY 2-digit year 23 MM 2-digit month 10 MON Abbreviated month name OCT MONTH Full month name OCTOBER DD 2-digit day of month 25 DY Abbreviated day name WED DAY Full day name WEDNESDAY HH24 Hour (00-23) 14 HH or HH12 Hour (01-12) 02 MI Minute (00-59) 30 SS Second (00-59) 22 FF Fractional Seconds (for TIMESTAMP) 123456 AM or PM Meridian indicator PM TZH Time zone hour offset -07 TZM Time zone minute offset 00 / . , - Punctuation and separators as used
Examples of TO_CHAR:
SELECT
TO_CHAR(SYSDATE, 'MM/DD/YYYY') AS "Simple Date", -- 10/25/2023
TO_CHAR(SYSDATE, 'Dy, Mon DD, YYYY') AS "Text Date", -- Wed, Oct 25, 2023
TO_CHAR(SYSDATE, 'HH24:MI:SS') AS "Military Time", -- 14:30:22
TO_CHAR(SYSDATE, 'HH:MI:SS AM') AS "12-Hour Time", -- 02:30:22 PM
TO_CHAR(SYSTIMESTAMP, 'DD-MON-YYYY HH24:MI:SS.FF3') AS "With Milliseconds" -- 25-OCT-2023 14:30:22.123
FROM dual;
- Converting Strings to Dates/Timestamps (TO_DATE, TO_TIMESTAMP)
The TO_DATE and TO_TIMESTAMP functions convert a string into a DATE or TIMESTAMP data type.
Syntax:
TO_DATE(string_column, 'format_model')
TO_TIMESTAMP(string_column, 'format_model')
Examples:
-- Converting a string to a DATE
INSERT INTO orders (order_date)
VALUES (TO_DATE('25-OCT-2023 02:30 PM', 'DD-MON-YYYY HH:MI AM'));
-- Converting a string with a custom format to a TIMESTAMP
SELECT TO_TIMESTAMP('2023/10/25 14:30:22.123', 'YYYY/MM/DD HH24:MI:SS.FF3')
FROM dual;
-- Converting a string with time zone
SELECT TO_TIMESTAMP_TZ('2023-10-25 14:30:22 US/Pacific', 'YYYY-MM-DD HH24:MI:SS TZR')
FROM dual;
Crucial Note: If you don't specify a format model for a string that doesn't match the NLS_DATE_FORMAT of your database, you will get an error. Always use an explicit format model for reliability.
- Default Display and NLS Settings
The way Oracle displays dates by default is controlled by the NLS_DATE_FORMAT and NLS_TIMESTAMP_FORMAT parameters. This is a common source of confusion.
· View your current settings:
SELECT * FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER IN ('NLS_DATE_FORMAT', 'NLS_TIMESTAMP_FORMAT', 'NLS_TIME_FORMAT');
· Change the format for your session:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF';
After running this, a simple SELECT SYSDATE FROM dual; will show the full date and time.
Summary and Best Practices
Your Goal Function to Use Example Display a date/time as a formatted string TO_CHAR TO_CHAR(order_date, 'MM/DD/YYYY') Convert a string into a DATE TO_DATE TO_DATE('10/25/2023', 'MM/DD/YYYY') Convert a string into a TIMESTAMP TO_TIMESTAMP TO_TIMESTAMP('2023-10-25 14:30:22.123', '...') Get the current date and time SYSDATE (for DATE) SYSTIMESTAMP (for TIMESTAMP) SELECT SYSTIMESTAMP FROM dual;
Key Takeaway: Never rely on implicit conversions. Always use TO_DATE, TO_TIMESTAMP, and TO_CHAR with an explicit format model to ensure your code is predictable, correct, and independent of session-level NLS settings.
Comments
Post a Comment