Stop 04 · Functions & ExpressionsTopics · 8/8

CAST and Type Conversion

Part of the SQL Roadmap

In short

A price imported as text, a timestamp you need as a plain date, an integer that should divide with decimals. CAST changes a value's type so SQL treats it the right way.

CAST converts a value from one type to another, such as text to a number or a timestamp to a date. Databases also convert types implicitly, which can hide bugs and stop indexes from being used.

The jaadu hands one large banknote across a shop counter and receives a neat fan of smaller notes in return.

Think of it like this

Changing a ₹500 note

You give the shopkeeper a ₹500 note and get five ₹100 notes back. Same value, different form, now usable for a ₹100 auto ride. CAST does that with data: '450' as text becomes 450 as a number you can add. But try to change a torn note and the shopkeeper refuses, just as CAST('abc' AS INTEGER) fails.

Watch it happen

Explicit vs implicit conversion

Explicit (CAST)

  • You say exactly which type you want
  • Errors show up early and clearly
  • Reader can see the conversion
  • Same behaviour across databases

Implicit (automatic)

  • Database guesses the type for you
  • MySQL turns 'abc' into 0 with only a warning
  • Can stop an index from being used
  • Behaviour differs between databases

Let SQL convert obvious literals like '2026-04-01' for a date column, but write CAST whenever you change a column's type on purpose.

The big ideas

What you need to know

CAST(value AS type)

The standard form: CAST('450' AS INTEGER), CAST(created_at AS DATE). Works everywhere, with type names that differ a little.

:: shortcut in PostgreSQL

'450'::integer and created_at::date are PostgreSQL shorthand. Handy, but not portable.

Timestamp to date

CAST(created_at AS DATE) drops the time, perfect for grouping orders by day.

Bad values fail

CAST('12a' AS INTEGER) errors in PostgreSQL. Clean or filter text before casting it.

See it in code

Walk through the code

sql
1SELECT2  CAST('450' AS INTEGER) + 50               AS as_number,3  '2026-04-01'::date + 7                    AS week_later,4  CAST(created_at AS DATE)                  AS order_day,5  CAST(amount AS NUMERIC(10, 2))            AS amount_2dp,6  CAST(order_id AS TEXT) || '-IN'           AS order_code,7  CAST(7 AS NUMERIC) / 2                    AS exact_half8FROM orders;

1/4Text becomes a number, so + does maths: 500.

PostgreSQL. In MySQL, cast to SIGNED instead of INTEGER, to CHAR instead of TEXT, use CONCAT instead of ||, and skip the :: shortcut. SQL Server uses VARCHAR and has TRY_CAST, which returns NULL instead of failing.

Save yourself the pain

Common mistakes

Oops

Storing amounts as text and casting them in every query.

Do this instead

Fix the column type once with ALTER TABLE ... TYPE NUMERIC USING amount::numeric.

Oops

WHERE CAST(created_at AS DATE) = '2026-04-01' on a big table.

Do this instead

Casting the column can stop an index being used. Prefer created_at >= '2026-04-01' AND created_at < '2026-04-02'.

Your mission

Convert with care

~20 min

Quick check

Which expression turns a timestamp column into just the date in standard SQL?

Go deeper

  1. 1Convert a text column of numbers into INTEGER and sort it correctly.
  2. 2Cast a timestamp to DATE to group by day.
  3. 3Learn PostgreSQL's :: shorthand and MySQL's CAST(x AS SIGNED / DECIMAL) forms.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge