Format dates in SAP HANA
How date formatting works in SAP HANA: what TO_VARCHAR and TO_DATE change, when to keep native date types, and how to share dates without ambiguity.
Formatting a date in SAP HANA usually means one of two things: turning a stored date or timestamp into text in a chosen layout, or reading text into a real date value. The functions are simple. The harder question is whether the converted result still means the same thing, sorts correctly and can be used by whoever receives it. This review covers both.
Short recommendation
Keep dates in native types (DATE, TIME, SECONDDATE or TIMESTAMP) for storage, filtering, joins and arithmetic. Convert to text only at the last step, when a person or an external file needs a specific layout. The main benefit of formatting is a predictable, readable output. The main tradeoff is that the result becomes a string: it no longer behaves like a date, and a numeric layout such as month/day/year can be read differently by different people and programs.
Leave the value unformatted when the receiver is another query, a reporting tool or an application that can handle date types itself. When text is unavoidable and the audience is mixed or unknown, the ISO-style year-month-day layout is the safer choice.
What formatting actually changes
In this context, "format" does not mean visual styling, a font or how a client tool happens to display a column. It means a data type conversion. TO_VARCHAR (or TO_NVARCHAR) takes a date or timestamp and returns a character string built from a format pattern. TO_DATE, TO_TIMESTAMP and TO_SECONDDATE go the other way and parse a string into a date-type value using a pattern that describes the input.
The stored value is not modified by a SELECT that formats it. Only the query result changes. A client tool may also display native date columns in its own layout. That is presentation only: the underlying value is the same, and the displayed layout depends on the tool's settings rather than on the database.
| Format element | Meaning |
| YYYY | Four-digit year |
| YY | Two-digit year |
| MM | Month number, 01–12 |
| MON | Abbreviated month name |
| DD | Day of month, 01–31 |
| HH24 | Hour, 00–23 |
| HH12 | Hour, 01–12 (use with AM or PM) |
| MI | Minutes |
| SS | Seconds |
| FF1–FF7 | Fractional seconds, 1 to 7 digits |
| AM / PM | Meridian indicator |
These elements are part of the documented format model for HANA's conversion functions. The supported list can differ between releases and between on-premise and cloud editions, so check the reference for your version before relying on less common elements.
Common conversions
-- Date to US-style text
SELECT TO_VARCHAR(DATE '2026-03-07', 'MM/DD/YYYY') AS us_date FROM DUMMY;
-- expected: 03/07/2026
-- Date with an abbreviated month name
SELECT TO_VARCHAR(DATE '2026-03-07', 'DD-MON-YYYY') AS named_month FROM DUMMY;
-- expected: 07-MAR-2026
-- Timestamp with a 12-hour clock
SELECT TO_VARCHAR(TIMESTAMP '2026-03-07 14:05:09', 'YYYY-MM-DD HH12:MI:SS AM') AS ts_text FROM DUMMY;
-- expected: 2026-03-07 02:05:09 PM
-- Text to date
SELECT TO_DATE('03/07/2026', 'MM/DD/YYYY') AS parsed FROM DUMMY;
-- Compact eight-character text to date
SELECT TO_DATE('20260307', 'YYYYMMDD') AS parsed FROM DUMMY;
The expected outputs follow from the documented meaning of each format element; they were not executed for this article. Run them on your own system to confirm, especially the letter case of month abbreviations and the AM/PM output.
If you only need one part of a date, extraction functions avoid string handling entirely. Examples include YEAR, MONTH, DAYOFMONTH, EXTRACT, and MONTHNAME or DAYNAME for English month and weekday names.
Where formatting causes problems
- Sorting: text in MM/DD/YYYY order sorts by month first, so dates from different years interleave. Year-month-day text sorts in chronological order.
- Filtering: comparing a formatted string to a literal is fragile and can stop the database from using the column efficiently. Compare the native column to a date literal instead, for example WHERE order_date >= DATE '2026-01-01'.
- Arithmetic: date functions such as ADD_DAYS and DAYS_BETWEEN expect date types. Formatted text has to be parsed back before it can be used.
- Ambiguity: 03/07/2026 is March 7 to most US readers but July 3 in many other regions. Numbers alone do not say which convention was used.
- Parsing mismatches: TO_DATE only succeeds when the pattern matches the input. Data from mixed sources often needs cleaning first.
- Two-digit years: YY drops the century, which loses information for anything long-lived or historical.
Dates stored as text in SAP application tables
Many SAP application tables keep dates as eight-character strings in YYYYMMDD order rather than as a native DATE column. These values sort correctly as text, but they need TO_DATE with the YYYYMMDD pattern before date functions or date-type comparisons will work. These columns may also hold placeholder values, such as all zeros, to mean no date. How a given HANA release converts such placeholders was not covered by the supplied evidence. Test this on your system rather than assuming a NULL or an error.
Will the result stay usable after copying or sharing?
Formatted output is ordinary text, so it copies and pastes exactly as produced. Nothing is reordered or substituted. The risks begin at the destination. Spreadsheets and import tools often try to interpret date-like text, and they may apply their own regional rules. That can swap day and month or turn the value into a number. How a particular spreadsheet or tool behaves was not tested here and depends on its settings. Check with a small sample before sending a full export.
For people reading the result, a layout with a spelled or abbreviated month name removes the day-month ambiguity. That helps human readers whatever tool they use. No evidence was supplied on how specific screen readers or other assistive technologies announce numeric versus named-month dates, so no claim is made here. If accessibility matters for the output, test it with the tools your audience uses.
Keep an unformatted version, either the native date or year-month-day text, whenever the data will be searched, edited, re-imported or processed by software. Keep the friendly layout for the final human-facing copy.
Gaps in the available evidence
No version-specific documentation, test results or measurements were supplied for this article. Version-dependent details are not covered here. These include the full set of format elements, the language of month and day names, whether default display formats can be changed per session, and how time zone conversions such as UTCTOLOCAL and LOCALTOUTC apply. Confirm these against the official reference for the HANA release you use.