Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

PostgreSQL to_char function

3 September 2020

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:
ParameterExplanation
9Value (without initial zeroes)
0Value (with leading zeros)
.Decimal
,Group splitter
PRA negative value in angle brackets
СSymbol
LSymbol of currency
DDecimal
GGroup splitter
MIMinus sign (for negative numbers)
PLSign plus (for positive numbers)
SGA plus/minus sign (for positive and negative numbers)
RNRoman numerals
THSerial number suffix
thSerial number suffix
V Shift in numbers
EEEEScientific notation

 

With dates

With dates, format_mask can be one of the following and can be used in many combinations.

ParameterExplanation
YYYY4-digit year
Y,YYY4-digit semicolon year
YYY  YY

Y

Last 3, 2 or 1 digit (and) years
IYYYThe 4-digit year according to ISO standard
IYY  IY

I

Last 3, 2 or 1 digit(s) of ISO year
QA quarter of the year (1, 2, 3, 4; JAN-MAR = 1)
ММMonth (01-12; JAN = 01)
MONAbbreviated name of the month in upper case
MonAbbreviated name of the month with a capital letter
monAbbreviated name of the month in lower case
MONTHThe name of the month in capital letters, completed with spaces up to 9 characters long
MonthThe name of the month with a capital letter, supplemented with spaces up to 9 characters long
monthThe name of the month in lowercase letters, supplemented with spaces up to 9 characters long
RMOne month with Roman numerals
rmMonth in lowercase Roman numerals
WWWeek of the year (1-53), where week 1 begins on the first day of the year
WWeek of the month (1-5), where week 1 begins on the first day of the month
IWISO Week of the year (01-53)
DAYThe name of the day in capital letters, completed with spaces up to 9 characters long
DayThe name of the day with a capital letter, completed with spaces up to 9 characters long
dayThe name of the day in lowercase letters, completed with spaces up to 9 characters long
DYAbbreviated name of the day in upper case
DyAbbreviated name of the day with a capital letter
dyAbbreviated name of the day in lowercase letters
DDDDay of the year (1-366)
IDDDDay of the year based on ISO year
DDDay of the month (01-31)
DDay of the week (1-7, where 1 = Sunday, 7 = Saturday)
IDDay of the week based on ISO year (1-7, where 1 = Monday, 7 = Sunday)
JJulian day; the number of days from midnight November 24, 4714 BC.
HHOne o’clock of the day (01-12)
HH12One o’clock of the day (01-12)
HH24One o’clock of the day (00-23)
MIOne minute (00-59)
SSOne second (00-59)
MSMillisecond (000-999)
USMicrosecond (000000-999999)
SSSSSeconds after midnight (0-86399)
am, AM, pm, or PMMeridian Indicator
a.m., A.M., p.m., or P.M.Meridian Indicator
ad, AD, a.d., or A.DAD indicator
bc, BC, b.c., or B.C.BC Indicator
TZName of the time zone in upper case
tzName of the time zone in lower case
CC2-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

 

 
Tags: , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB 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...

Preamble​​MS 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...

Preamble​​Atom 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...

Preamble​​MongoDB 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...

Preamble​​SQLShell 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...

Preamble​​Writing 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...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM 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....

Preamble​​If 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...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...