Feeling stuck with Segment? Say 👋 to RudderStack.

SVG
Log in

Queries for Casting - PostgreSQL

String to Date and Timestamp

Strings containing date or time information, from user inputs or logs, are not optimized for date-time operations in databases like PostgreSQL. Proper date or timestamp data types offer efficiency, precision, and more functionalities.

How to convert string to date or timestamp

PostgreSQL supports various kinds of date/time related formats such as: `

date

`, `

time

`, `

timestamp

`, their time zone-aware versions, and interval for durations. You can cast a string to any of these data types.

There are three ways to convert string to date or timestamp in Postgres

  1. Using the `::` operator
  2. Using the `CAST()` function
  3. Using `TO_DATE()` and `TO_TIMESTAMP()` functions

Using the `

::

` operator:

Convert a string to a date:

SQL
SELECT '2023-07-14'::date;

Convert a string to a timestamp:

SQL
SELECT '2023-07-14 10:30:00'::timestamp;

Using the `

CAST()

` function:

Convert a string to a date:

SQL
SELECT CAST('2023-07-14' AS date);

Convert a string to a timestamp:

SQL
SELECT CAST('2023-07-14 10:30:00' AS timestamp);

Using `

TO_DATE()

` and `

TO_TIMESTAMP()

` functions:

These functions are particularly useful when the string format isn't in the default '

YYYY-MM-DD

' or doesn't match PostgreSQL's expected timestamp structure. Using these functions, you can specify the format of the string according to which it can be converted to date/timestamp.

String to date conversion using `

to_date(date, format)

`:

The TO_DATE function in PostgreSQL is used to convert strings into dates. Its syntax is TO_DATE(text, text),it converts string to date according to the given format and returns the converted date.

Usage examples -`

to_date

`:

Converting a non-standard date string:

SQL
SELECT TO_DATE('14 July, 2023', 'DD Month, YYYY');

Converting a day of the month - month - year format:

SQL
SELECT TO_DATE('14-07-2023', 'DD-MM-YYYY');

Converting month name, day, and year:

SQL
SELECT TO_DATE('July 14, 2023', 'Month DD, YYYY');

Converting abbreviated month:

SQL
SELECT TO_DATE('14 Jul 2023', 'DD Mon YYYY');

Year and day of the year:

SQL
SELECT TO_DATE('2023, 195', 'YYYY, DDD');

year - weak of the year - day of the week (ISO 8601 format):

SQL
SELECT TO_DATE('2023-W28-5', 'IYYY-IW-ID');

Custom format with text:

SQL
SELECT TO_DATE('Day: 14 of Month: July in Year: 2023', 'Day: DD of Month: Month in Year: YYYY');
String to timestamp conversion using `

to_timestamp(datetime, format)

`:

The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text). It converts string to timestamp according to the specified format and returns the timestamp.

Usage examples -`

to_timestamp

`:

Convert a string with specific timestamp format:

SQL
SELECT TO_TIMESTAMP('14/07/2023 10:30:00', 'DD/MM/YYYY HH24:MI:SS');

Converting day-month-year with time:

SQL
SELECT TO_TIMESTAMP('14-07-2023 15:20:30', 'DD-MM-YYYY HH24:MI:SS');

Using abbreviated month and 12-hour format:

SQL
SELECT TO_TIMESTAMP('14 Jul 2023 03:20:30 PM', 'DD Mon YYYY HH:MI:SS AM');

Year, day of the year, and time:

SQL
SELECT TO_TIMESTAMP('2023, 195 15:20:30', 'YYYY, DDD HH24:MI:SS');

Including timezone offset:

SQL
SELECT TO_TIMESTAMP('14-07-2023 15:20:30 +03', 'DD-MM-YYYY HH24:MI:SS TZH');

Using ISO 8601 format with time:

SQL
SELECT TO_TIMESTAMP('2023-W28-5 15:20:30', YYYY-IW-ID HH24:MI:SS');

Custom format with text and time:

SQL
SELECT TO_TIMESTAMP('Day: 14 of Month: July in Year: 2023 at 15:20:30', 'Day: DD of Month: Month in Year: YYYY at HH24:MI:SS');

Usage examples:

Which method is the best for date/timestamp casting:

  • For straightforward, brief type conversions, the :: operator is handy.
  • For portability and clarity, especially in complex SQL scripts or functions, `

    CAST()

    ` is a good choice.
  • When dealing with intricately formatted strings that need to be transformed to date/timestamp types, choose `

    TO_DATE()

    ` or `

    TO_TIMESTAMP()

    `.

Common errors to watch out for:

  • Incorrect date format: Ensure the string format aligns with what PostgreSQL or the function expects.
  • Mismatched data types: Be wary of strings that don't match the expected structure.
  • Out-of-Range Values: Such as '2023-02-30'.

Ambiguous Casting: Ambiguities might arise when casting overlaps between data types.

String to numeric

In PostgreSQL strings can be converted to integer or double using

CAST

function or the

::

annotation. Both are equivalent and you can choose whichever you like.

Usage example:

If a column contains money data you should keep in mind that floating point numbers should not be used to handle money due to the potential for rounding errors.

A money value can be cast to numeric without loss of precision. Conversion to other types could potentially lose precision, and must also be done in two stages:

Number::money

Avoid converting it to float due to potential rounding errors

Sign Up For Free And Start Sending Data

Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.