Open In App

PostgreSQL TO_TIMESTAMP() Function

Last Updated : 26 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In PostgreSQL, managing and manipulating date and time values is important, especially when they are stored as strings. The to_timestamp function allows us to convert textual representations of dates and times into a valid timestamp format and making it easier to work with them in queries, calculations or comparisons.

In this article, we'll explore the to_timestamp function in PostgreSQL, its syntax, common format patterns and practical examples.

PostgreSQL TO_TIMESTAMP() Function

  • The to_timestamp function in PostgreSQL is used to convert a string or numeric value into a timestamp data type.
  • This function is especially useful when dealing with date and time information in formats that need to be transformed into a PostgreSQL timestamp for further operations or storage.

Syntax:

to_timestamp(string text, format text) → timestamp

Explanation:

  • string: The input value that needs to be converted to a timestamp. It can be a string representing a date and time or a numeric value.
  • format: A string that specifies the format of the input string or number. This format helps PostgreSQL understand how the input value should be parsed and converted into a timestamp.

Common Format Patterns

The to_timestamp function converts a string into a timestamp by following the format string you've provided. Failing to match the input string with the correct format will lead to unexpected results.

You can construct the format strings and use the below template patterns to format date and time values.

PatternDescription
HHHour of Day (01-12)
HH12Hour of Day (01-12)
HH24Hour of Day (00-23)
YLast Digit of Year
YYLast Two Digits of the Year
YYYLast Three Digits of the Year
YYYYLast Four Digits of the Year
Y,YYYYear in Four Digits with Comma
MIMinute(00-59)
SSSecond(00-59)
MSMillisecond (000-999)
USMicrosecond (000000-999999)
SSSSSeconds Past Midnight (0-86399)
AM
 or A.M.
 or PM
 or P.M.
Meridian Indicator (Uppercase)
am
 or a.m.
 or pm
 or p.m.
Meridian Indicator (Lowercase)
BC
 or B.C.
 or AD
 or A.D.
Era Indicator (Uppercase)
bc
 or b.c.
 or ad
 or a.d.
Era Indicator (Lowercase)
IYYYISO Year (4 and more digits)
IYYLast Three Digits of ISO Year
IYLast Two Digits of ISO Year
ILast Digits of ISO Year
MONTHFull Uppercase Month Name
MonthFull Mixed-Case Month Name
monthFull Lowercase Month Name
monAbbreviated Lowercase Month Name
MMMonth Number (01-12)

Examples of Using to_timestamp

Exampe 1: Converting a Simple Date String

  • The query SELECT to_timestamp('04-12-2002', 'DD-MM-YYYY'); converts the date string '04-12-2002', which is in the format 'DD-MM-YYYY', into a timestamp.
  • The to_timestamp function takes two arguments: the date string and the format mask. In this case, the format mask 'DD-MM-YYYY' tells PostgreSQL how to interpret the string, resulting in a valid timestamp that can be used in date and time operations.

Query:

SELECT to_timestamp('04-12-2002', 'DD-MM-YYYY');

Output:

Simple-data-string
Simple data string

Explanation:

  • In this example, '04-12-2002' is the input string representing a date.
  • The format string 'DD-MM-YYYY' specifies how the day, month, and year are arranged.
  • PostgreSQL uses this format to convert the string into a timestamp

Exampe 2: Converting a Date and Time String

  • The query SELECT to_timestamp('2002-12-04 12:30:00', 'YYYY-MM-DD HH24:MI:SS'); converts the string '2002-12-04 12:30:00', which includes both date and time, into a timestamp.
  • The format mask 'YYYY-MM-DD HH24:MI:SS' specifies how the string should be parsed, resulting in a precise timestamp suitable for detailed time operations.

Query:

SELECT to_timestamp('2002-12-04 12:30:00', 'YYYY-MM-DD HH24:MI:SS');

Output:

data-and-time-string
Date and time string

Explanation:

  • In this case, the input string '2002-12-04 12:30:00' includes both the date and time.
  • The format string 'YYYY-MM-DD HH24:MI' ensures that PostgreSQL can correctly interpret the data as year, month, day, hour, minute, and second.

Exampe 3: Using a Compact Date Format

  • The query SELECT to_timestamp('20021204', 'YYYYMMDD') converts the date string '20021204', which is in the compact format 'YYYYMMDD' into a timestamp.
  • The format mask 'YYYYMMDD' tells PostgreSQL how to interpret the string, resulting in a timestamp that represents December 4, 2002.

Query:

SELECT to_timestamp('20021204', 'YYYYMMDD');

Output:

date-string
Date String

Explanation:

The input string '20021204' is a compact date format. By providing the format string 'YYYYMMDD', PostgreSQL is able to correctly parse the year, month, and day. The timestamp will be generated with a default time of 00:00:00.

Conclusion

The to_timestamp function in PostgreSQL is a versatile tool for converting date and time values stored as strings into proper timestamp data types. By specifying the correct format, you can convert a wide variety of date and time formats into timestamp values that can be used for calculations, comparisons, or storage.

FAQs

What is the to_timestamp function used for in PostgreSQL?

The to_timestamp function is used to convert a string representation of a date and/or time into a timestamp data type in PostgreSQL. This is particularly useful when importing or processing data stored as text.

What happens if the input string doesn’t match the format in to_timestamp?

If the input string and the format do not match, PostgreSQL will either return an error or produce incorrect results. It’s essential to ensure that the format string matches the structure of the input string.

Can I use to_timestamp for strings that include only time?

Yes, you can use to_timestamp for strings that include only time. You just need to provide a time format (such as HH24:MI:SS) in the format string.

What is the default time when converting a date string without a time component?

If the input string contains only a date without a time component, PostgreSQL will default to 00:00:00 for the time part.

Can I convert custom date formats using to_timestamp?

Yes, you can convert custom date formats as long as you provide a format string that accurately describes how the input string is structured.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg