CAST()

SQL · Date Functions

Theory

CAST converts a value from one data type to another: CAST('2026-11-08' AS DATE) turns text into a date, CAST('45000' AS INTEGER) turns text into a number, and CAST(order_ts AS DATE) drops the time part of a timestamp. The syntax CAST(value AS type) is standard SQL and works almost everywhere. PostgreSQL and Snowflake also accept the shorthand value::date.

Casting matters because the type decides behaviour. Text '900' sorts after text '1200' (because '9' > '1'), but as numbers 900 < 1200. A timestamp compared with a date may miss rows from later in the day unless you cast it first. If the text cannot be converted, most databases raise an error; SQL Server and Snowflake offer TRY_CAST, which returns NULL instead. SQLite is dynamically typed, so CAST(x AS INTEGER) works, but to turn a timestamp into a date you use date(x).

Why we use it

Real data often arrives with the wrong type: numbers stored as text, timestamps where you need dates. CAST makes the type explicit so sorting, maths, comparisons and joins behave the way you expect.

When to use it

Use CAST when cleaning staged data, comparing a timestamp to a date, or doing maths on numeric text. For non-ISO date text use TO_DATE with a mask; for formatting output use TO_CHAR. Use TRY_CAST on messy input.

How it works

  1. Value of the 'wrong' type arrives
  2. Write CAST(value AS target_type)
  3. Can the value be converted? Yes: Return the new value. No: Error (NULL with TRY_CAST).
  4. Sorting, maths and comparisons now follow the new type
  5. Clean, correctly typed column

Syntax

CAST(value AS type)

Example

SELECT CAST(hire_date AS DATE) 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