Dates in DB2 and Oracle: Same Data Type, Different Behavior

Dates in DB2 and Oracle: Same Data Type, Different Behavior

ost enterprise applications today use database systems to persist data, and they utilize these data for various organizational needs. Often these applications need to store, retrieve, and manipulate date and time values to perform various business processes. Commercial databases, such as IBM’s DB2 Universal Database Ver. 8.2 (DB2) and Oracle 10g (Oracle), provide extensive native support for date and time data types, which reduces the load of manipulating these data types and increases performance. Being from different software houses, they provide different levels of support for these data types, as well as differing storage, retrieval, and manipulation functionalities.

This article provides an analysis of the basic differences between the date and time data types in DB2 and Oracle, focusing on DATE, TIME (available only in DB2), and TIMESTAMP in particular. It begins by introducing the data types in each database and moves on to cover their storage needs, internal representation, and values ranges. Finally, it discusses the differences in their date arithmetic operations. Understanding these differences can come in handy, especially when you are migrating data from one database to another.

Date and Time Types in DB2 and Oracle

DB2 provides three basic data types for working with date and time: DATE, TIME, and TIMESTAMP (see Table 1).

Data Type Example Constituent Elements Description
DATE 2005-01-01 Year, month, and day Represents January 1, 2005
TIME 13:00:00 Hour, minutes, and seconds Represents 1 PM
TIMESTAMP 2005-01-01.12.00.00.000000 Year, month, day, hour, minute, second, and microseconds Represents January 1, 2005, 12:00 PM
Table 1. Date and Time Types in DB2

In DB2, TIMESTAMP is a combination of DATE and TIME values, with the time value having precision up to fractional microseconds (one-millionth of a second).

Oracle provides only two basic data types for working with date and time information: DATE and TIMESTAMP. (Oracle does offer TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE as well, but they are just variations of the TIMESTAMP type and are beyond the scope of this discussion.)

Oracle has no equivalent to DB2’s TIME data type. Instead, it stores time with DATE values (see Table 2).

Data Type Example Constituent Elements Description
DATE 2005-01-01 12.00.00 Year, month, day, hour, minute, and seconds Represents January 1, 2005, 12:00 PM
TIMESTAMP 2005-01-01.12.00.00.000000000 Year, month, day, hour, minutes, seconds, and nanoseconds Represents January 1, 2005, 12:00 PM
Table 2. Date and Time Types in Oracle

In Oracle, the TIMESTAMP type is an extension of the DATE type, with the time value having precision up to nanoseconds (one-billionth of a second).

MIN and MAX Values

Table 3 presents the minimum and maximum values for DATE, TIME, and TIMESTAMP in DB2 and Oracle.

DB2
Data Type Minimum Value Maximum Value
DATE 0001-01-01 9999-12-31
TIME 00.00.00 24.00.00
TIMESTAMP 0001-01-01.01.00.00.00.000000 9999-12-31.24.00.00.000000
Oracle
Data Type Minimum Value (excluding Year 0) Maximum Value (excluding Year 0)
DATE – 4712-01-01.00.00.00 9999-12-31
TIMESTAMP – 4712-01-01.00.00.00.000000000 9999-12-31.23.59.59.999999999
Table 3. Minimum and Maximum Values for DB2 and Oracle

DB2 and Oracle differ in the minimum values they can store in date and time data types. DB2 stores DATE values starting from Common Era (CE) whereas Oracle starts from year -4712, which is Before Common Era (BCE).

Oracle can store TIMESTAMP values with a variable precision ranging from seconds to nanoseconds. If you don’t specify the needed precision while declaring the TIMESTAMP type, the Oracle database manager will default to microseconds.

DB2 supports precision up to microseconds under the TIMESTAMP data type. However, unlike Oracle, the precision in DB2 is not variable.

Internal Representation

Internal representation defines how database systems store each data type and its storage needs. DB2 and Oracle differ in how they store date and time data types.

In DB2, DATE values need four bytes for storage. Each byte carries two digits as packed decimal digits. The first two bytes store the year, the third byte stores the month, and the fourth byte stores the day. (A packed decimal digit carries sign information in its last nibble (half byte). But DB2 won’t store sign information in the last nibble with DATE, TIME, and TIMESTAMP values.)

DB2 stores TIME values in a 24-hour clock format and needs three bytes for storage. The first byte stores the hour, the second stores the minute, and the last stores the seconds?each of them as packed decimal digits.

The TIMESTAMP data type is a combination of date, time, and milliseconds, and it needs ten bytes for storage. The first four bytes carry date, the next three bytes carry time, and the last three bytes carry the microseconds as packed decimal digits.

Table 4 presents the internal representation of the TIMESTAMP value 01/01/2005 11:30:30.000000 in DB2.

Year Month Day Hour Minute Second Milliseconds
Byte 1 2 3 4 5 6 7 8 9 10
Value 2005 01 01 11 30 30 000000
Note: In disk, the values are stored as binary. To make them easier to understand, the table represents the actual values rather than the binary representations.
Table 4. Minimum and Maximum Values for DB2 and Oracle

The first four bytes represent the DATE part, the next three represent the TIME part, and the last three represent microseconds.

In Oracle, DATE values need seven bytes for storage. The storage needs of TIMESTAMP values vary between seven and 11 bytes (variable length data type). For DATE, the first byte stores century, the second stores year, the third stores month, the fourth stores day, the fifth stores hour, the sixth stores minutes, and the seventh stores seconds. For TIMESTAMP values, the first seven bytes are similar to those of the DATE value, and the last four bytes represent the fractional second’s value?which is up to a precision of nanoseconds. However, if you store a DATE value in a TIMESTAMP column, it needs only seven bytes for storage, like a DATE value.

Table 5 presents the internal representation of the TIMESTAMP value 01/01/2005 11:30:30.9999999999 in Oracle.

Century Year Month Day Hour Minute Seconds Nanoseconds
Byte 1 2 3 4 5 6 7 8 9 10 11
Value 120 105 1 1 12 31 31 999999999
Note: In disk, the values are stored as binary. To make them easier to understand, the table represents the actual values rather than the binary representations.
Table 5. Internal Representation of TIMESTAMP in Oracle

You’re probably pondering a couple of obvious questions about the internal representation of DATE values in Oracle:

  1. Why are the century and year values off by 100 rather than 20 and 5, respectively?
  2. Why are the hour, minutes, and seconds values off by 1?

When Oracle stores DATE and TIMESTAMP values, it provides an offset value of 100 for century and year. This offset helps Oracle to store DATE values from BCE. It adds an offset value of 100 to century and year for DATE values in CE, while subtracting century and year values from the offset value of 100 for DATE values in BCE.

DB2 and Oracle provide rich sets of facilities for manipulating date and time data types.Both provide native support for these data types, which eliminates the need to transform them into other types in order to support arithmetic operations like addition and subtraction. As with storage, both differ in date arithmetic operations as well.

Date Arithmetic in DB2

DB2 provides support for three date arithmetic operations:

  • Subtracting DATE, TIME, or TIMESTAMP value from corresponding types to yield DURATION type (e.g., two DATE values can be subtracted, yielding the interval between them as DATE DURATION.)
  • Addition and subtraction of similar DURATIONS from DATE, TIME, and TIMESTAMP (e.g., time duration can be added or subtracted from TIME or TIMESTAMP values only.)
  • Addition and subtraction of labeled durations (of compatible time) from DATE, TIME, and TIMESTAMP (e.g., labeled duration of MICROSECONDS can be added or subtracted from TIMESTAMP values only.)

DURATION values are available only during execution time and are represented as packed decimal digits. In contrast to DB2 date and time types, duration carries the sign information to represent positive and negative duration. For example, SELECT DATE ('2005-03-01') - DATE ('2005-02-01') FROM SYSIBM.SYSDUMMY1 gives 00000100 as duration, which means a difference of one month between the two date values.

In DB2, date arithmetic is similar to numerical arithmetic using packed decimal digits. In the above example, DB2 would subtract the packed decimal representation of operand 1 from the packed decimal representation of operand 2. (Click here for more information on SYSIBM.SYSDUMMY1.)

Be cautious when you add or subtract two durations as it may lead to obvious wrong results. For example, if you add a duration of 1 day to a duration of 99 days, DB2 will return 1 month as the duration instead of 100 days?because DB2 does a packed decimal addition rather than a date addition.

Suppose you need to find the duration between two date or timestamp values such as days, months, or years. In such cases, first convert each operand into days or months or years using DB2 functions like DAYS or MONTH or YEAR, and then perform the arithmetic operation. For example, SELECT DAYS (DATE ('2005-03-01')) - DAYS (DATE ('2005-02-01')) FROM SYSIBM.SYSDUMMY1 returns the duration as days between two dates. SELECT MONTH (DATE ('2005-03-01')) - MONTH (DATE ('2005-02-01')) FROM SYSIBM.SYSDUMMY1 returns the duration as number of months between two dates. SELECT YEAR(DATE('2005-03-01')) - YEAR(DATE('2005-02-01')) FROM SYSIBM.SYSDUMMY1 returns the duration as number of years between two dates.

To add a day, month, or year to a date or timestamp value, you can use labeled types provided by DB2. For example, SELECT DATE('2005-04-01') + 10 DAYS FROM SYSIBM.SYSDUMMY1 adds 10 days to the given date. SELECT DATE('2005-04-01') + 2 MONTHS FROM SYSIBM.SYSDUMMY1 adds two months to the given date. SELECT DATE('2005-04-01') + 1 YEAR FROM SYSIBM.SYSDUMMY1 adds a year to the given date value. Labeled types are easy to use, they are self-documenting, and they make the code more readable.

Date Arithmetic in Oracle

Like DB2, Oracle can perform three date arithmetic operations:

  • Subtract two DATE values, yielding the interval between the two as days
  • Add an interval to a DATE or TIMESTAMP value (For example, adding six months to the current date yields a DATE value of six months past the current date.)
  • Add or subtract two interval values, yielding an INTERVAL YEAR TO MONTH or INTERVAL DAY TO SECOND data type.

In contrast to DB2, which returns the interval between two DATE values as a duration type, Oracle returns the interval between two date values as days. For example, SELECT TO_DATE('01-MAR-04') - TO_DATE('01-FEB-04') from DUAL returns 29 days rather than 0000 Years, 01 months, and 00 days as it would in DB2. (Click here for more information on the DUAL table in Oracle.)

Oracle doesn’t have functions for DAYS, MONTH, and YEAR like DB2 does. In Oracle, the intervals between two DATE and TIMESTAMP values are always returned as days. To get the interval in months, it provides a built-in function called MONTHS_BETWEEN. For example, SELECT MONTHS_BETWEEN (TO_DATE('01-DEC-04'),TO_DATE('01-FEB-04') ) FROM DUAL returns 10 months. If the second argument is later than the first, the result will be a negative value.

Similar to labeled types in DB2, if you want to add ‘X’ months to the current date or timestamp, first convert ‘X’ into an INTERVAL YEAR TO MONTH data type or INTERVAL DAY TO SECOND data type, and then add the intervals to date as in the following example:

SELECT TO_DATE('01-MAR-04')  + NUMTOYMINTERVAL(1,'MONTH')  FROM DUALSELECT TO_DATE('01-MAR-04')  + NUMTOYMINTERVAL(1,'YEAR')  FROM DUALSELECT TO_DATE('01-MAR-04')  + NUMTODSINTERVAL(2,'HOUR')  FROM DUAL

Instead of using the built-in functions to convert a numeric value to an interval type, you can declare an interval type explicitly as INTERVAL ‘X’ DAY or INTERVAL ‘X’ MONTH. For example, SELECT SYSDATE + INTERVAL '25' DAY FROM DUAL adds 25 days to the current date. Similarly SELECT SYSDATE + INTERVAL '2' MONTH FROM DUAL adds two months to the current date.

Why You Need to Know

You have seen the basic differences between date and time data types in DB2 and Oracle. A clear understanding of these differences will equip you to overcome most of the challenges related to migrating data from one database system to another?especially those related to chronological information. However, do not overlook other differences between the two, like formatting, time zone information, etc., which are also critical in real world scenarios.

devx-admin

devx-admin

Share the Post:
Performance Camera

iPhone 15: Performance, Camera, Battery

Apple’s highly anticipated iPhone 15 has finally hit the market, sending ripples of excitement across the tech industry. For those considering upgrading to this new

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years,

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Chip Overcoming

iPhone 15 Pro Max: Overcoming Chip Setbacks

Apple recently faced a significant challenge in the development of a key component for its latest iPhone series, the iPhone 15 Pro Max, which was unveiled just a week ago.

Performance Camera

iPhone 15: Performance, Camera, Battery

Apple’s highly anticipated iPhone 15 has finally hit the market, sending ripples of excitement across the tech industry. For those considering upgrading to this new model, three essential features come

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years, as reported by energy analytics

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These