Formatting & Casting: FORMAT, CONVERT, CAST
Display dates and numbers your way with FORMAT, change data types with CAST and CONVERT (with date styles), and know which one to pick.
The database stores 2025-08-20, but an Indian report wants 20/08/2025, a poster wants 20 Aug 2025, and an e-mail wants Wednesday. Separately, you sometimes need to change a value's type — turn the text '123' into a number, or a number into text to glue onto a sentence. That is formatting and casting.
Definition
Formatting changes how a value looks (it returns text). Casting changes a value's data type (e.g. VARCHAR → INT, VARCHAR → DATE).
| Function | Definition |
|---|---|
FORMAT(value, format [, culture]) | Returns the value as a string laid out according to a format pattern ('dd/MM/yyyy') or standard code ('N', 'C', 'P'). Optional culture like 'en-US' or 'en-IN'. SQL Server only. |
CONVERT(data_type, value [, style]) | Converts a value to another data type; the optional style number controls the date/text layout. SQL Server only. |
CAST(value AS data_type) | Converts a value to another data type. ANSI-standard — works in every database. |
Syntax
sql
FORMAT(value, format_string [, culture])
CONVERT(data_type, value [, style])
CAST(value AS data_type)FORMAT — show a date your way
sql
SELECT
FORMAT(CAST('2025-08-20' AS DATE), 'dd/MM/yyyy') AS indian_style, -- '20/08/2025'
FORMAT(CAST('2025-08-20' AS DATE), 'dd MMM yyyy') AS poster, -- '20 Aug 2025'
FORMAT(CAST('2025-08-20' AS DATE), 'MMMM yyyy') AS month_year, -- 'August 2025'
FORMAT(CAST('2025-08-20' AS DATE), 'dddd') AS day_name; -- 'Wednesday'FORMAT returns a string, and the pattern is case-sensitive: MM = month, mm = minutes.
Format specifiers (sample value 2025-08-20 18:55:45):
| Spec | Description | Result | Spec | Description | Result | |
|---|---|---|---|---|---|---|
d | short date | 8/20/2025 | yy | year, 2 digits | 25 | |
D | full date | Wednesday, August 20, 2025 | yyyy | year, 4 digits | 2025 | |
dd | day, 2 digits | 20 | HH | hour (24h) | 18 | |
ddd | short day name | Wed | hh | hour (12h) | 06 | |
dddd | full day name | Wednesday | mm | minutes | 55 | |
MM | month, 2 digits | 08 | ss | seconds | 45 | |
MMM | short month | Aug | tt | AM / PM | PM | |
MMMM | full month | August |
FORMAT — numbers too
| Expression | Result (culture en-US) | Meaning |
|---|---|---|
FORMAT(1234.56, 'N') | '1,234.56' | N = number with thousand separators (2 decimals) |
FORMAT(1234.56, 'N0') | '1,235' | N0 = number, 0 decimals (rounded) |
FORMAT(1234.56, 'C') | '$1,234.56' | C = currency |
FORMAT(0.256, 'P') | '25.60 %' | P = percent (×100) |
Tip:
FORMAT(1234.56, 'C', 'en-IN')shows Indian rupees (₹). The output is always text, so you can't do maths on it afterwards.
CONVERT — change type (+ style)
sql
SELECT
CONVERT(INT, '124') AS to_int, -- 124
CONVERT(VARCHAR, 750) AS to_text, -- '750'
CONVERT(VARCHAR, CAST('2025-08-20' AS DATE), 103) AS uk_india, -- '20/08/2025'
CONVERT(VARCHAR, CAST('2025-08-20' AS DATE), 101) AS us; -- '08/20/2025'Common CONVERT date styles (sample 2025-08-20 18:55:45):
| Style | Format | Example |
|---|---|---|
| 1 | mm/dd/yy | 08/20/25 |
| 101 | mm/dd/yyyy | 08/20/2025 |
| 3 | dd/mm/yy | 20/08/25 |
| 103 | dd/mm/yyyy | 20/08/2025 |
| 23 | yyyy-mm-dd | 2025-08-20 |
| 8 | hh:mm:ss | 18:55:45 |
Rule of thumb: styles below 100 give a 2-digit year, 100+ give a 4-digit year.
CAST — change type (ANSI standard)
| Expression | Result | Note |
|---|---|---|
CAST('123' AS INT) | 123 | text → number |
CAST(750 AS VARCHAR) | '750' | number → text |
CAST('2025-08-20' AS DATE) | 2025-08-20 | text → date |
CAST(3.516 AS INT) | 3 | drops the decimals — no rounding! |
Example — CAST to build a sentence
sql
SELECT
CONCAT(name, ' scored ', CAST(runs AS VARCHAR(10)), ' runs') AS line
FROM players;| line |
|---|
| Virat Kohli scored 750 runs |
| Rohit Sharma scored 500 runs |
| Jos Buttler scored 850 runs |
| MS Dhoni scored 350 runs |
| David Warner scored 700 runs |
| Jasprit Bumrah scored 20 runs |
(CONCAT would convert the number automatically, but with the + operator in SQL Server name + ' scored ' + runs fails — SQL tries to turn the text into an INT. CAST makes the intent explicit.)
CAST vs CONVERT vs FORMAT — which one?
| CAST | CONVERT | FORMAT | |
|---|---|---|---|
| Purpose | change the data type | change type + date style | custom text output |
| Portability | standard SQL — works everywhere | SQL Server only | SQL Server only |
| Syntax | CAST(value AS type) | CONVERT(type, value, style) | FORMAT(value, format, culture) |
| Speed | fast | fast | flexible, but slower on big tables |
| Returns | the new type | the new type | always a string |
Use CAST by default, CONVERT when you need a specific date style, and FORMAT for presentation-only output on small result sets.
Common mistakes
- ❌
FORMAT(d, 'dd/mm/yyyy')→ minutes instead of month! ✅'dd/MM/yyyy'. - ❌ Expecting
CAST(3.9 AS INT)= 4. ✅ It truncates to 3; useROUNDfirst. - ❌ Sorting by a FORMAT result (
'20/08/2025') — text sorts alphabetically, not by date. ✅ Sort by the original date column. - ❌
CAST('hello' AS INT)→ conversion error. ✅ Validate first (ISDATEfor dates) or useTRY_CAST, which returns NULL instead of failing.
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Format a date | FORMAT(d, 'dd/MM/yyyy') / CONVERT(VARCHAR, d, 103) | DATE_FORMAT(d, '%d/%m/%Y') | TO_CHAR(d, 'DD/MM/YYYY') |
| Change type | CAST / CONVERT | CAST / CONVERT(v, type) | CAST / v::type |
Note: FORMAT and CONVERT are SQL Server features, so their examples below are shown as SQL Server-only code. CAST between numbers and text works in the browser playground.
Key points to remember
- Formatting changes how a value looks (returns text); casting changes its data type.
- FORMAT(value, format [, culture]) returns a string; patterns are case-sensitive — MM = month, mm = minutes.
- FORMAT also formats numbers: 'N' number, 'N0' no decimals, 'C' currency, 'P' percent.
- CONVERT(type, value, style) is SQL Server-only; style 103 = dd/mm/yyyy, 101 = mm/dd/yyyy, 23 = yyyy-mm-dd.
- CAST(value AS type) is ANSI-standard and works in every database.
- CAST to INT truncates decimals (3.516 → 3) — it does not round.
- Default to CAST; use CONVERT for date styles and FORMAT for display-only text (it is slower).
Try it yourself
SQL Server only: FORMAT dates and numbers
DECLARE @d DATETIME2 = '2025-08-20 18:55:45';
SELECT
FORMAT(@d, 'dd/MM/yyyy') AS indian_style, -- 20/08/2025
FORMAT(@d, 'dd MMM yyyy') AS poster, -- 20 Aug 2025
FORMAT(@d, 'MMMM yyyy') AS month_year, -- August 2025
FORMAT(@d, 'dddd') AS day_name, -- Wednesday
FORMAT(@d, 'hh:mm tt') AS start_time, -- 06:55 PM
FORMAT(1234.56, 'N') AS num, -- 1,234.56
FORMAT(1234.56, 'C') AS money_us, -- $1,234.56
FORMAT(0.256, 'P') AS pct; -- 25.60 %SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
SQL Server only: CONVERT with date styles
SELECT
name,
debut,
CONVERT(VARCHAR(10), debut, 103) AS debut_ddmmyyyy, -- 20/04/2008
CONVERT(VARCHAR(10), debut, 101) AS debut_mmddyyyy, -- 04/20/2008
CONVERT(VARCHAR(8), debut, 3) AS debut_ddmmyy, -- 20/04/08
CAST('2025-08-20' AS DATE) AS as_date -- 2025-08-20
FROM player_profiles;SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Try it: CAST between text and numbers
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: a readable debut line
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
