Using STRING_TO_TABLE() in PostgreSQL

In PostgreSQL, we can use the string_to_table() function to return a set of rows, each containing a part of the string. The string is split based on the specified delimiter.

If we specify a null delimiter, then each character becomes a separate row in the output. If the delimiter string is empty, then the whole string is returned in a single row.

We also have the option of turning a specific substring into null if required.

Continue reading

How to Set the datestyle Variable for the Current Session in PostgreSQL

PostgreSQL has a datestyle variable that specifies the display format for date and time values, as well as the rules for interpreting ambiguous date input values. 

We can set the date/time style with the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client.

Below is an example of using the SET datestyle command to change the datestyle for the current session.

Continue reading

Fix Error “cannot subscript type text because it does not support subscripting” in PostgreSQL

If you’re getting a PostgreSQL error that reads something like “cannot subscript type text because it does not support subscripting” when selecting data from a database, it’s probably because you’re trying to perform some sort of array operation against a non array value.

The above error specifically mentions text data but we could get the same error when using a different data type, like an integer, character varying, etc.

To fix this issue, be sure to run the array operations against actual arrays. If working with non array data, then don’t use array operations against that data.

Continue reading

How to Create an Empty Array When Using the ARRAY Constructor

In PostgreSQL we can use the ARRAY constructor to create an array. When we do this, we provide the array elements as a comma separated list, enclosed in square brackets. Postgres then works out the data type based on the array elements.

But what if we want to create an empty array?

Creating an empty array can cause issues if we don’t explicitly specify the type. We need to specify the type. Below is an example of creating an empty array in PostgreSQL using the ARRAY constructor.

Continue reading

Fix “function generate_subscripts(text[], integer, integer) does not exist” in PostgreSQL

If you’re getting a PostgreSQL error that reads something like “function generate_subscripts(text[], integer, integer) does not exist“, it’s probably because your third argument is of the wrong type when using the generate_subscripts() function.

The above error specifically implies that an integer was passed as the third argument, but it must be a boolean value.

The third argument of the generate_subscripts() function is optional, but if passed, it must be a boolean expression.

To fix this error, either pass a boolean value as the third argument, or eliminate the third argument altogether.

Continue reading

Using the <@ Operator in PostgreSQL

In PostgreSQL, the <@ operator checks to see whether the second array contains the first array. That is, whether or not the array on the right of the operator contains all elements in the array to the left.

The function returns a Boolean result: It returns true if the second array contains the first array, and false if it doesn’t. If the result is unknown, it returns NULL.

Continue reading