In PostgreSQL, you can use the to_char()
function to output numbers in a given format. This includes appending the number with the ordinal indicator.
For example 1, 2, 3 becomes 1st, 2nd, 3rd.
Continue readingIn PostgreSQL, you can use the to_char()
function to output numbers in a given format. This includes appending the number with the ordinal indicator.
For example 1, 2, 3 becomes 1st, 2nd, 3rd.
Continue readingWhen 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 readingIn PostgreSQL, you can use the to_char()
to output a number in a certain format, including with a plus or minus sign to indicate whether it’s a positive or negative number.
To do this, use one of S
, MI
, PL
, or SG
in your format string.
Another option is to use PR
in order to enclose any negative values in angle brackets.
This page contains the full list of template patterns and template pattern modifiers that can be used when formatting numbers in PostgreSQL.
Continue readingIt can be easy to forget that the T-SQL FORMAT()
function provides locale-aware formatting. Locale-aware means that the locale can affect the results. In other words, the exact output you get will depend on the locale.
By default, the function uses the language of the current session to determine the locale. However, this can be overridden by passing a “culture” argument to the function. Doing this allows you to provide results for a particular locale without having to change the language of the current session.
This article contains examples of how locale can affect the results when using the FORMAT()
function in SQL Server.
Here’s a quick way to add brackets around negative numbers in SQL Server when using the FORMAT()
function.
The goal here is that brackets are only added to negative values. No brackets are added to positive values or zeros. Also, the brackets replace any minus sign that would otherwise be displayed (in other words, no minus sign is displayed when the brackets are used).
Although formatting is often best left to the presentation layer, there may be cases that dictate a T-SQL solution in SQL Server. In such cases, hopefully this article helps.
This article provides a reference for the standard numeric format specifiers that can be used when formatting numbers using the FORMAT()
function in SQL Server. Examples included.
Some of the examples use precision specifiers (these consist of one or two digits appended to the format specifier). Precision specifiers can be a value from 0 to 99, which specifies the precision of the result. How it works depends on the format specifier being used. For some format specifiers, it will specify the total number of digits in the result, for others it will specify the number of decimal places. In other cases it will be ignored altogether.
Perhaps one of the lesser-known features of the FORMAT()
function in SQL Server is one that enables you to apply conditional formatting to a number.
It’s more of a .NET feature than it is a SQL Server (or T-SQL) feature, but SQL Server/T-SQL supports it all the same, allowing you to take full advantage of the ability to apply conditional formatting to numbers.
It all comes down to the format string you pass to the FORMAT()
function.
You can pass a format string that specifies how the number should be formatted, depending on whether it’s positive, negative, or zero.