How to Format Numbers in PostgreSQL

When working with Postgres, you can use the to_char() function to output numbers in a given format.

The way it works is that you provide two arguments. The first argument is the number to be formatted. The second argument determines how it is formatted.

The return value is text.

Depending on your requirements, you might prefer to convert the number to the appropriate data type, for example using the cast() function.

Examples of both methods are below.

Continue reading

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