PostgreSQL

PostgreSQL To_char Timestamp With Timezone

PostgreSQL formatting methods include a useful collection of tools for translating different data types (date/time, integer, floating-point, numeric) to formatted strings and translating formatted strings back to unique data types. Henceforth, sometimes we need to convert time zones as well. The timing is always recorded in UTC in PostgreSQL timestamps for time zone data form, but it is displayed by default in the browser, session, or user’s local time. One of its helper functions we’ve come to rely on is the TO_CHAR() method, which allows timestamps and timestamps with timezone, among other forms, and enables you to arrange the pieces of a timestamp however you like. A timestamp, a double-precision, the duration, a number, or a numeric value can all be converted to a string using the PostgreSQL TO_CHAR() method. There seems to be a single-argument method, ‘to_timestamp’, that takes a double-precision argument and transforms from Unix epoch to timestamp using time zone. We’ll show you how to do something about this in this post. Let’s take a closer look at to_char() first.

Syntax:

General Syntax for to_char() function is as follows:

>> To_char( expression, format );

The TO_CHAR() method in PostgreSQL needs two assertions:

  • Expression: A timestamp, a duration, a number, a dual precision, or a numeric value that is translated to a string as per a particular format may all be used as expressions.
  • Format: The style in which the output string will be shown. The format can be different according to the expression type, e.g., number, date.

There are two timestamp types available in PostgreSQL:

  • Timestamp: without a timezone.
  • Timestamptz: with timezone.

And here’s the issue: the standard timestamp data form is ignorant of time zones. And it’s a SQL necessity (how it could have occurred seems beyond). Our main focus is to learn to_Char() timestamp with a timezone. To start working on the PostgreSQL with the ‘to_char()’ function, open the PostgreSQL command-line shell and deliver the parameter values for the obligatory server, database, port number, username, and password. Leave these considerations unfilled if you need to consume the default designated parameters as shown in the below image.

To_char() For String Number

To understand the concept of the to_Char() function using timestamp with timezone, you have first to try the example of string numbers. So we have a number ‘1897,’ and we will be converting it to the ‘9999.99’ format using the below query. From the output below, you can see that the string number has been converted into the specified format.

>> SELECT to_char(1897,9999.99);

Here is another illustration for conversion. This time we have converted a number into a different format having ‘comma’ in it. The character ‘G’ will be used to specify a comma.

>> SELECT to_char(367.78, ‘9G999.99’);

To_char Timestamp with TimeZone

To understand the Timestamp with timezone concept, let’s consider a simple example. Suppose you are in ‘Pakistan’, so your timezone must be ‘PKT’ right now.

Example 01:

Let’s try to fetch the current timestamp in the SELECT query while converting it to Date-time format, as shown in the query below. The term ‘TZ’ is used to give back the current Timezone. The output displays the day, date, time, and timezone.

>> SELECT to_char(CURRENT_TIMESTAMP, 'Day Mon dd, yyyy HH12:MI AM (TZ)');

Let’s change our timezone to ‘Europe/Rome’.

>> SET TimeZone= ‘Europe/Rome’;

You will get a different time, date, and timezone upon trying the same SELECT query, as shown.

Example 02:

When you specify the TimeZone in the SELECT query, then the output will not show the current time zone as per the below output.

>> SELECT to_char(CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Jerusalem', ‘yyyy HH12:MI AM (TZ)');

Example 03:

Let’s create a quick table named ‘time’ with two fields. One is of TIMESTAMP type, and the other is TIMESTAMPTZ type.

>> CREATE TABLE time( without_timezone TIMESTAMP, with_timezone TIMESTAMPTZ);

Now let us check the current timezone which we have been using in our system using the SHOW command in the shell as follows:

>> SHOW timezone;

Now you have to insert the current values of the date and time of the current timezone which you have been using on your device in the table ‘time’ by using the ‘now()’ function as shown below.

Now you can fetch the record from the table ‘time’ using the SELECT query as below. The column ‘without_timezone’ shows the current date and time without a timezone, while the column ‘with_timezone’ shows the local time with the timezone completely.

Let’s change the timezone to ‘US/EASTERN’ from the below query.

>> SET SESSION TIME ZONE ‘US/EASTERN’;

Now let’s check the table again. You will see how the value of the ‘with_timezone’ column has been displayed according to timezone ‘US/EASTERN’, but the value of ‘without_timezone’ is the same as it was before.

Example 04:

Let’s have some more examples for the to_char() method. Assume the same above table ‘time’. We will be converting the column ‘without_timezone’ value to a string that is made up of hours, minutes, seconds, and timezone. Let’s try the SELECT query using the to_char() method for converting column value ‘without_timezone’. We have mentioned ‘TZ’ in our query, but it will not show the timezone because the column value does not consist of the timezone. The stated-below command gives the output:

>> SELECT to_char(without_timezone, 'HH12:MI:SS TZ') FROM time;

Now let’s try the very same query in the case of the other column ‘with_timezone’, to convert it to the string of hours, minutes, seconds, and timezone. This time it will show the timezone with time as well using the below query.

>> SELECT to_char(with_timezone, 'HH12:MI:SS TZ') FROM time;

Conclusion:

Since the with/without time zone problem affects more than just table partitioning, I recommend that you should use the time zone type whenever practicable. Almost all guidelines discussed how to do time-dependent purging in PostgreSQL using local hours. A proper, time zone-sensitive solution adds little complications but could save you from trouble in the future.

About the author

Aqsa Yasin

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.