Efficient Teradata Date Calculations Avoiding INTEGER Values

Teradata internally stores the date as INTEGER.

Calculate dates after January 1st, 1900 using this formula:

((year- 1900) * 10000) + (month * 100) + day

While this technique is compact and efficient, I recommend avoiding it due to its poor readability and inaccuracy for dates before 1900.

Here are some more useful Teradata Date Calculations:

First day of the month:
CURRENT_DATE – (EXTRACT(DAY FROM CURRENT_DATE) – 1)

Last day of the previous month:
CURRENT_DATE – EXTRACT(DAY FROM CURRENT_DATE)

Last day of the month, n months ago:
ADD_MONTHS(CURRENT_DATE – (EXTRACT(DAY FROM CURRENT_DATE) – 1), -n + 1) – 1

The first day of the month, n months ago:
ADD_MONTHS(CURRENT_DATE – (EXTRACT(DAY FROM CURRENT_DATE) – 1), -n)

First day of the year:
ADD_MONTHS(CURRENT_DATE – (EXTRACT(DAY FROM CURRENT_DATE) – 1), -EXTRACT(MONTH FROM CURRENT_DATE) + 1)

Day of week (1-7; 1 = Monday, 2 = Tuesday, …)
(CURRENT_DATE – DATE ‘0001-01-01’) MOD 7 + 1

The aforementioned computations utilize the EXTRACT function.

Avoid using substrings for date calculations.

Here are three methods for determining the first day of any month based on a comprehensive test table.

The CPU times utilized vary significantly. While the computations with INTEGER values (second row of the table) are the most efficient, they should be avoided due to their lack of clarity.

CPU TimeStatement
114,40thedate – EXTRACT(DAY FROM thedate ) + 1
74,10thedate  / 100 * 100 + 1 (DATE)
166,50thedate  – SUBSTRING(thedate  FROM 1 FOR 8) || ’01’ (DATE)

Related Services

⚡ Need Help Optimizing Your Data Platform?

We cut data platform costs by 30–60% without hardware changes. 25+ years of hands-on tuning experience.

Explore Our Services →

📋 Considering a Move From Teradata?

Get a personalized migration roadmap in 2 minutes. We have migrated billions of rows from Teradata to Snowflake, Databricks, and more.

Free Migration Assessment →

📊 Data Platform Migration Survey

Help us map where the industry is heading. Results are public — see what others chose.

1. What is your current data platform?

2. Where are you migrating to (or evaluating)?

Migrating FROM
Migrating TO

Thanks for voting! Share this with your network.

Follow me on LinkedIn for daily insights on data warehousing and platform migrations.

Stay Ahead in Data Warehousing

Get expert insights on Teradata, Snowflake, BigQuery, Databricks, Microsoft Fabric, and modern data architecture — delivered to your inbox.

Leave a Comment

DWHPro

Expert network for enterprise data platforms. Senior consultants, project teams built for your challenge — across Teradata, Snowflake, Databricks, and more.

📍Vienna, Austria & Jacksonville, Florida

Quick Links
Services Team Teradata Book Blog Contact Us
Connect
LinkedIn → [email protected]
Newsletter

Join 4,000+ data professionals.
Weekly insights on Teradata, Snowflake & data architecture.