How to_char() Works in PostgreSQL

In Postgres, to_char() is a data type formatting function that converts its first argument to a string.

The format of the string is determined by the second argument.

The to_char() function can be used to do the following conversions:

  • time stamp to string
  • interval to string
  • integer to string
  • real/double precision to string
  • numeric to string
Continue reading

Set a Default Value for a Column in SQLite: DEFAULT Constraint

When creating a table in SQLite, you have the option of adding constraints to each column.

One such constraint is the DEFAULT constraint.

The DEFAULT constraint allows you to specify a value to be used in the event no value is supplied for that column when a new row is inserted.

If you don’t use a DEFAULT clause, then the default value for a column is NULL.

Continue reading

Add a Generated Column to a Table in SQLite

You can add a generated column to an existing table in SQLite by using the ALTER TABLE statement.

SQLite’s implementation of the ALTER TABLE statement is very limited, but it does allow you to add a column – including generated columns.

Generated columns (also known as “computed columns”) are columns that get their value from an expression that computes values from other columns.

Continue reading

How to Create a Computed Column in SQLite

Generated column support was added to SQLite in version 3.31.0, which was released on 22 January 2020.

Generated columns and computed columns are the same thing. They are columns whose values are a function of other columns in the same row.

In SQLite, generated columns are created using the GENERATED ALWAYS column-constraint when creating or altering the table.

There are two types of generated column; STORED and VIRTUAL. Only VIRTUAL columns can be added when altering a table. Both types can be added when creating a table.

Continue reading