- Installation
- Guides
- Data Import & Export
- CSV Import
- CSV Export
- Parquet Import
- Parquet Export
- Query Parquet
- HTTP Parquet Import
- S3 Parquet Import
- Meta Queries
- Python
- Install
- Execute SQL
- Jupyter Notebooks
- SQL on Pandas
- Import From Pandas
- Export To Pandas
- SQL on Arrow
- Import From Arrow
- Export To Arrow
- DuckDB with Ibis
- SQL Editors
- Documentation
- Connect
- Data Import
- Client APIs
- Overview
- Python
- R
- Java
- C
- Overview
- Startup
- Configure
- Query
- Data Chunks
- Values
- Types
- Prepared Statements
- Appender
- Table Functions
- Replacement Scans
- API Reference
- C++
- Node.js
- Wasm
- ODBC
- CLI
- SQL
- Introduction
- Statements
- Overview
- Select
- Insert
- Delete
- Update
- Create Schema
- Create Table
- Create View
- Create Sequence
- Create Macro
- Drop
- Alter Table
- Copy
- Export
- Query Syntax
- Data Types
- Expressions
- Functions
- Overview
- Numeric Functions
- Text Functions
- Pattern Matching
- Date Functions
- Timestamp Functions
- Time Functions
- Interval Functions
- Date Formats
- Date Parts
- Blob Functions
- Nested Functions
- Utility Functions
- Indexes
- Aggregates
- Window Functions
- Samples
- Information Schema
- Configuration
- Pragmas
- Extensions
- Development
- Sitemap
- Why DuckDB
- FAQ
- Code of Conduct
- Live Demo
Casting refers to the process of changing the type of a row from one type to another. The standard SQL syntax for this is CAST(expr AS typename)
. DuckDB also supports the easier to type shorthand expr::typename
, which is also present in PostgreSQL.
-- integers [1, 2, 3]
SELECT CAST(i AS VARCHAR), i::DOUBLE;
-- "1", "2", "3"
-- 1.0, 2.0, 3.0
The exact behavior of the cast depends on the source and destination types. For example, when casting from VARCHAR
to any other type, the string will be attempted to be converted.
Not all casts are possible. For example, it is not possible to convert an INTEGER
to a DATE
. Casts may also throw errors when the cast could not be successfully performed. For example, trying trying to cast the string 'hello'
to an INTEGER
will result in an error being thrown.
Implicit Casting
In many situations, the system will add casts by itself. This is called implicit casting. This happens for example when a function is called with an argument that does not match the type of the function, but can be casted to the desired type.
Consider the function SIN(DOUBLE)
. This function takes as input argument a column of type DOUBLE
, however, it can be called with an integer as well: SIN(1)
. The integer is converted into a double before being passed to the SIN
function.
Generally, implicit casts only cast upwards. That is to say, we can implicitly cast an INTEGER
to a BIGINT
, but not the other way around.