SQL Data-Types Casting

Comparing Data Types and Casting in SQL

Here's a comparison of data types and casting functions in PostgreSQL, BigQuery (Standard SQL), and ISO SQL, specifically focusing on string and datetime handling, including casting functions and date truncation.


Data Types

FeaturePostgreSQLBigQuery (Standard SQL)ISO SQL (SQL-2016)
String TypesTEXT, VARCHAR(n), CHAR(n)STRINGCHAR(n), VARCHAR(n), CLOB
Datetime TypesDATE, TIME, TIMESTAMP, TIMESTAMPTZ, INTERVALDATE, DATETIME, TIMESTAMP, TIMEDATE, TIME, TIMESTAMP, INTERVAL
Time ZonesTIMESTAMPTZ for UTC conversionTIMESTAMP (implicitly UTC), DATETIME (no timezone)TIMESTAMP WITH TIME ZONE, TIMESTAMP WITHOUT TIME ZONE
IntervalsINTERVAL type (e.g., INTERVAL '1 day')No explicit INTERVAL, but can use TIMESTAMP_DIFF and TIMESTAMP_ADDINTERVAL type (similar to PostgreSQL)

Casting Functions

FunctionPostgreSQLBigQuery (Standard SQL)ISO SQL
Explicit CastCAST(value AS target_type) or value::target_typeCAST(value AS target_type) or SAFE_CASTCAST(value AS target_type)
String to Integer'123'::INTEGER or CAST('123' AS INTEGER)CAST('123' AS INT64) or SAFE_CASTCAST('123' AS INTEGER)
String to Float'12.34'::FLOAT or CAST('12.34' AS FLOAT)CAST('12.34' AS FLOAT64)CAST('12.34' AS FLOAT)
String to DateCAST('2024-01-01' AS DATE) or '2024-01-01'::DATECAST('2024-01-01' AS DATE) or PARSE_DATE('%Y-%m-%d', '2024-01-01')CAST('2024-01-01' AS DATE)
String to TimestampCAST('2024-01-01 12:34:56' AS TIMESTAMP) or '2024-01-01 12:34:56'::TIMESTAMPCAST('2024-01-01 12:34:56' AS TIMESTAMP) or PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '2024-01-01 12:34:56')CAST('2024-01-01 12:34:56' AS TIMESTAMP)
Date to StringTO_CHAR(DATE '2024-01-01', 'YYYY-MM-DD')FORMAT_DATE('%Y-%m-%d', DATE '2024-01-01')CAST(DATE '2024-01-01' AS CHAR)

Date Truncation (DATE_TRUNC and equivalents)

FunctionPostgreSQLBigQuery (Standard SQL)ISO SQL
Truncate DateDATE_TRUNC('month', DATE '2024-03-15')2024-03-01DATE_TRUNC(DATE '2024-03-15', MONTH)2024-03-01TRUNC(DATE '2024-03-15', 'MONTH')
Truncate TimestampDATE_TRUNC('hour', TIMESTAMP '2024-03-15 14:45:00')2024-03-15 14:00:00TIMESTAMP_TRUNC(TIMESTAMP '2024-03-15 14:45:00', HOUR)2024-03-15 14:00:00 UTCTRUNC(TIMESTAMP '2024-03-15 14:45:00', 'HOUR')
Truncate to YearDATE_TRUNC('year', DATE '2024-03-15')2024-01-01DATE_TRUNC(DATE '2024-03-15', YEAR)2024-01-01TRUNC(DATE '2024-03-15', 'YEAR')

Extracting Date Components

FunctionPostgreSQLBigQuery (Standard SQL)ISO SQL
Extract YearEXTRACT(YEAR FROM DATE '2024-03-15')EXTRACT(YEAR FROM DATE '2024-03-15')EXTRACT(YEAR FROM DATE '2024-03-15')
Extract MonthEXTRACT(MONTH FROM DATE '2024-03-15')EXTRACT(MONTH FROM DATE '2024-03-15')EXTRACT(MONTH FROM DATE '2024-03-15')
Extract DayEXTRACT(DAY FROM DATE '2024-03-15')EXTRACT(DAY FROM DATE '2024-03-15')EXTRACT(DAY FROM DATE '2024-03-15')
Extract HourEXTRACT(HOUR FROM TIMESTAMP '2024-03-15 14:45:00')EXTRACT(HOUR FROM TIMESTAMP '2024-03-15 14:45:00')EXTRACT(HOUR FROM TIMESTAMP '2024-03-15 14:45:00')

Adding and Subtracting Dates

FunctionPostgreSQLBigQuery (Standard SQL)ISO SQL
Add DaysDATE '2024-03-15' + INTERVAL '5 days'DATE_ADD(DATE '2024-03-15', INTERVAL 5 DAY)DATE '2024-03-15' + INTERVAL '5' DAY
Subtract DaysDATE '2024-03-15' - INTERVAL '5 days'DATE_SUB(DATE '2024-03-15', INTERVAL 5 DAY)DATE '2024-03-15' - INTERVAL '5' DAY
Add MonthsDATE '2024-03-15' + INTERVAL '1 month'DATE_ADD(DATE '2024-03-15', INTERVAL 1 MONTH)DATE '2024-03-15' + INTERVAL '1' MONTH
Add HoursTIMESTAMP '2024-03-15 14:45:00' + INTERVAL '2 hours'TIMESTAMP_ADD(TIMESTAMP '2024-03-15 14:45:00', INTERVAL 2 HOUR)TIMESTAMP '2024-03-15 14:45:00' + INTERVAL '2' HOUR

Summary

  • PostgreSQL has a flexible casting system (value::type syntax) and rich date manipulation functions with INTERVAL and DATE_TRUNC.
  • BigQuery follows a more explicit syntax for casting (SAFE_CAST) and date functions (TIMESTAMP_TRUNC, DATE_ADD).
  • ISO SQL is more general, supporting standard CAST, EXTRACT, and INTERVAL, but specific syntax like TRUNC(date, 'unit') is not as widely supported in real-world databases.
Originally posted:
Filed Under:
data
sql