TO_CHAR()

SQL · Date Functions

Theory

TO_CHAR does the opposite of TO_DATE: it turns a date (or number) into formatted text. TO_CHAR(hire_date, 'DD-Mon-YYYY') on 2023-07-01 gives '01-Jul-2023', and 'Month YYYY' gives 'July 2023'. The format mask controls the output: DD for day, MM for month number, Mon or Month for the name, YYYY for year, HH24:MI for time.

TO_CHAR is available in Oracle, PostgreSQL and Snowflake. MySQL uses DATE_FORMAT(d, '%d-%b-%Y'), SQL Server uses FORMAT(d, 'dd-MMM-yyyy') or CONVERT with a style number, and SQLite uses strftime('%d-%m-%Y', d), which has no month-name code. The key thing to remember is that the result is text, not a date: it is perfect for labels in a report or an export file, but if you sort or compare it, '01-Jul-2023' and '15-Mar-2021' will be ordered alphabetically, not by time.

Why we use it

People read dates in local styles (08-11-2026, Nov 2026), but databases store them in one internal form. TO_CHAR formats dates exactly as a report, invoice or export file needs them.

When to use it

Use it at the very end, for display, labels or file exports. Do not use it for filtering, sorting or grouping by time; keep the real date (or DATE_TRUNC) for that and format only in the final SELECT.

How it works

  1. Real DATE value, e.g. 2023-07-01
  2. Pick the display mask: 'DD-MM-YYYY', 'Mon YYYY'
  3. Database fills each code with the date's parts
  4. Return a text string such as '01-07-2023'
  5. Still need to sort by date? Yes: Sort by the original date column. No: Use the text for display.
  6. Human-friendly date labels

Syntax

TO_CHAR(date, 'format')

Example

SELECT TO_CHAR(hire_date, 'DD-Mon-YYYY') FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic