REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
PostgreSQL to_char function
PostgreSQL to_char function converts a number or date to a string.
Syntax of to_char function in PostgreSQL
to_char( value, format_mask )
Parameters and function arguments
- value – A number, a date to be converted to a string.
- format_mask – The format that will be used to convert the value to a string. The format_mask is different from whether you convert numbers or dates. Let’s have a look.
- WITH NUMBERS – With format_mask numbers can be one of the following and can be used in many combinations:
Parameter | Explanation |
---|---|
9 | Value (without initial zeroes) |
0 | Value (with leading zeros) |
. | Decimal |
, | Group splitter |
PR | A negative value in angle brackets |
С | Symbol |
L | Symbol of currency |
D | Decimal |
G | Group splitter |
MI | Minus sign (for negative numbers) |
PL | Sign plus (for positive numbers) |
SG | A plus/minus sign (for positive and negative numbers) |
RN | Roman numerals |
TH | Serial number suffix |
th | Serial number suffix |
V | Shift in numbers |
EEEE | Scientific notation |
With dates
With dates, format_mask can be one of the following and can be used in many combinations.
Parameter | Explanation |
---|---|
YYYY | 4-digit year |
Y,YYY | 4-digit semicolon year |
YYY YY Y | Last 3, 2 or 1 digit (and) years |
IYYY | The 4-digit year according to ISO standard |
IYY IY I | Last 3, 2 or 1 digit(s) of ISO year |
Q | A quarter of the year (1, 2, 3, 4; JAN-MAR = 1) |
ММ | Month (01-12; JAN = 01) |
MON | Abbreviated name of the month in upper case |
Mon | Abbreviated name of the month with a capital letter |
mon | Abbreviated name of the month in lower case |
MONTH | The name of the month in capital letters, completed with spaces up to 9 characters long |
Month | The name of the month with a capital letter, supplemented with spaces up to 9 characters long |
month | The name of the month in lowercase letters, supplemented with spaces up to 9 characters long |
RM | One month with Roman numerals |
rm | Month in lowercase Roman numerals |
WW | Week of the year (1-53), where week 1 begins on the first day of the year |
W | Week of the month (1-5), where week 1 begins on the first day of the month |
IW | ISO Week of the year (01-53) |
DAY | The name of the day in capital letters, completed with spaces up to 9 characters long |
Day | The name of the day with a capital letter, completed with spaces up to 9 characters long |
day | The name of the day in lowercase letters, completed with spaces up to 9 characters long |
DY | Abbreviated name of the day in upper case |
Dy | Abbreviated name of the day with a capital letter |
dy | Abbreviated name of the day in lowercase letters |
DDD | Day of the year (1-366) |
IDDD | Day of the year based on ISO year |
DD | Day of the month (01-31) |
D | Day of the week (1-7, where 1 = Sunday, 7 = Saturday) |
ID | Day of the week based on ISO year (1-7, where 1 = Monday, 7 = Sunday) |
J | Julian day; the number of days from midnight November 24, 4714 BC. |
HH | One o’clock of the day (01-12) |
HH12 | One o’clock of the day (01-12) |
HH24 | One o’clock of the day (00-23) |
MI | One minute (00-59) |
SS | One second (00-59) |
MS | Millisecond (000-999) |
US | Microsecond (000000-999999) |
SSSS | Seconds after midnight (0-86399) |
am, AM, pm, or PM | Meridian Indicator |
a.m., A.M., p.m., or P.M. | Meridian Indicator |
ad, AD, a.d., or A.D | AD indicator |
bc, BC, b.c., or B.C. | BC Indicator |
TZ | Name of the time zone in upper case |
tz | Name of the time zone in lower case |
CC | 2-digit century |
The to_char function can be used in future versions of PostgreSQL
PostgreSQL 11, PostgreSQL 10, PostgreSQL 9.6, PostgreSQL 9.5, PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4.
Let’s take a look at some examples of to_char functions to see how to_char can be used in PostgreSQL.
Example with numbers
Below are numerical examples of the to_char function.
SELECT to_char(1918, '9999.99');
--Result: 1918.00
SELECT to_char(1814.7, '9G999.99');
--Result: 1,814.70
SELECT to_char(1810.7, 'L9G999.99'); -Result: 1,814.70;
--Result: $ 1,810.70
SELECT to_char(1810.7, 'L9G999');
--Result: $ 1,811
SELECT to_char(141, '9 9 9');
--Result: 1 4 1
SELECT to_char(123, '00999');
--Result: 00123
Example with dates
Below are examples of the dates of the to_char function.
SELECT to_char(date '2019-04-23', 'YYYY/MM/DD');
--Result: 2019/04/23
SELECT to_char(date '2019-04-23', 'MMDDYY');
--Result: 042319
SELECT to_char(date '2019-04-23', 'Month DD, YYYYY');
--Result: April 23, 2019
PostgreSQL tutorial – functions
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...