Modeling Time: The _REAL Date Pattern
Purpose: To understand Epic’s ingenious solution for perfect chronological sorting.
The Problem with Standard Dates
Section titled “The Problem with Standard Dates”Imagine you have four patient encounters on the same day. How do you sort them chronologically when they all have the same date? Epic’s _REAL
pattern solves this with mathematical elegance.
Notice how all encounters share the same CONTACT_DATE
, but PAT_ENC_DATE_REAL
adds decimal precision: 64869.0, 64869.01, 64869.02, 64869.03. This guarantees perfect chronological ordering.
Decoding the _REAL Format
Section titled “Decoding the _REAL Format”The _REAL
format has two parts:
- Integer part: Days since December 31, 1840
- Decimal part: Sequence number for same-day events
Let’s prove the epoch date:
Why December 31, 1840? This predates modern computing by over a century—it’s a MUMPS convention from the 1960s that Epic inherited and maintains for backward compatibility.
The Power of Decimal Sequencing
Section titled “The Power of Decimal Sequencing”The decimal portion isn’t a timestamp—it’s a sequence number:
Where _REAL Dates Appear
Section titled “Where _REAL Dates Appear”These 13 columns appear wherever chronological precision matters:
- PAT_ENC_DATE_REAL: Order of patient encounters
- ORD_DATE_REAL: Precise timing of clinical orders
- CONTACT_DATE_REAL: Exact sequence of system interactions
The Sorting Guarantee
Section titled “The Sorting Guarantee”The critical rule: _REAL
dates are the ONLY reliable way to sort events chronologically. Here’s why:
In this case they match, but that’s pure luck—CSN_IDs happened to be assigned in chronological order. In production systems, you cannot rely on this coincidence.
Uniqueness Properties
Section titled “Uniqueness Properties”Epic guarantees that _REAL
values are unique within a patient’s record, making them perfect for identifying specific events.
Working with _REAL Dates
Section titled “Working with _REAL Dates”1. Convert to Standard Dates
-- Convert _REAL to readable dateSELECT PAT_ENC_DATE_REAL, DATE('1840-12-31', '+' || CAST(PAT_ENC_DATE_REAL AS INT) || ' days') as standard_dateFROM pat_enc;
2. Extract Components
-- Separate date and sequence partsSELECT PAT_ENC_DATE_REAL, CAST(PAT_ENC_DATE_REAL AS INT) as days_since_epoch, ROUND((PAT_ENC_DATE_REAL - CAST(PAT_ENC_DATE_REAL AS INT)) * 100, 2) as sequence_numberFROM pat_enc;
3. Always Sort by _REAL
-- The golden rule for chronological queriesSELECT * FROM encountersORDER BY PAT_ENC_DATE_REAL; -- Always correct
-- Never rely on thisORDER BY CONTACT_DATE, PAT_ENC_CSN_ID; -- Might be wrong
The Time Component Mystery
Section titled “The Time Component Mystery”One clarification: the decimal part does NOT encode time of day:
Actual times are stored in separate _TIME
or _DTTM
columns. The decimal is purely for sequencing.
Pattern Recognition
Section titled “Pattern Recognition”You can identify _REAL
columns by:
- Column name ends with
_REAL
- Data type is REAL (floating point)
- Values are large numbers (60000+) with optional decimals
- Usually paired with a human-readable date column
Key Takeaways
Section titled “Key Takeaways”_REAL
dates count days since December 31, 1840 (Epic’s epoch)- The decimal portion provides sequence numbers for same-day events
- This pattern appears in 13 columns across the database
_REAL
dates are the ONLY reliable way to sort events chronologically- Values are guaranteed unique within a patient’s record
- The decimal is NOT a timestamp—actual times are stored separately
- Always use
_REAL
columns for chronological sorting when available