PostgreSQL: Data Type Formatting Functions
Data Type Formatting Functions
There are various PostgreSQL formatting functions available for converting various data types (date/time, integer, floating point, numeric) to formatted strings and for converting from formatted strings to specific data types. Before discussing the formatting functions we will discuss various patterns available for formatting date and time values.
Patterns for Date/Time Formatting
Pattern | Description |
---|---|
HH | hour of day (01-12) |
HH12 | hour of day (01-12) |
HH24 | hour of day (00-23) |
MI | minute (00-59) |
SS | second (00-59) |
MS | millisecond (000-999) |
US | microsecond (000000-999999) |
SSSS | seconds past midnight (0-86399) |
AM, am, PM or pm | meridiem indicator (without periods) |
A.M., a.m., P.M. or p.m. | meridiem indicator (with periods) |
Y,YYY | year (4 and more digits) with comma |
YYYY | year (4 and more digits) |
YYY | last 3 digits of year |
YY | last 2 digits of year |
Y | last digit of year |
IYYY | ISO year (4 and more digits) |
IYY | last 3 digits of ISO year |
IY | last 2 digits of ISO year |
I | last digit of ISO year |
BC, bc, AD or ad | era indicator (without periods) |
B.C., b.c., A.D. or a.d. | era indicator (with periods) |
MONTH | full upper case month name (blank-padded to 9 chars) |
Month | full capitalized month name (blank-padded to 9 chars) |
month | full lower case month name (blank-padded to 9 chars) |
MON | abbreviated upper case month name (3 chars in English, localized lengths vary) |
Mon | abbreviated capitalized month name (3 chars in English, localized lengths vary) |
mon | abbreviated lower case month name (3 chars in English, localized lengths vary) |
MM | month number (01-12) |
DAY | full upper case day name (blank-padded to 9 chars) |
Day | full capitalized day name (blank-padded to 9 chars) |
day | full lower case day name (blank-padded to 9 chars) |
DY | abbreviated upper case day name (3 chars in English, localized lengths vary) |
Dy | abbreviated capitalized day name (3 chars in English, localized lengths vary) |
dy | abbreviated lower case day name (3 chars in English, localized lengths vary) |
DDD | day of year (001-366) |
IDDD | ISO day of year (001-371; day 1 of the year is Monday of the first ISO week.) |
DD | day of month (01-31) |
D | day of the week, Sunday(1) to Saturday(7) |
ID | ISO day of the week, Monday(1) to Sunday(7) |
W | week of month (1-5) (The first week starts on the first day of the month.) |
WW | week number of year (1-53) (The first week starts on the first day of the year.) |
IW | ISO week number of year (01 - 53; the first Thursday of the new year is in week 1.) |
CC | century (2 digits) (The twenty-first century starts on 2001-01-01.) |
J | Julian Day (integer days since November 24, 4714 BC at midnight UTC) |
Q | quarter (ignored by to_date and to_timestamp) |
RM | month in upper case Roman numerals (I-XII; I=January) |
rm | month in lower case Roman numerals (i-xii; i=January) |
TZ | upper case time-zone name |
tz | lower case time-zone name |
Patterns for Numeric Formatting
Pattern | Description |
---|---|
9 | value with the specified number of digits |
0 | value with leading zeros |
. (period) | decimal point |
, (comma) | group (thousand) separator |
PR | negative value in angle brackets |
S | sign anchored to number (uses locale) |
L | currency symbol (uses locale) |
D | decimal point (uses locale) |
G | group separator (uses locale) |
MI | minus sign in specified position (if number < 0) |
PL | plus sign in specified position (if number > 0) |
SG | plus/minus sign in specified position |
RN | Roman numeral (input between 1 and 3999) |
TH or th | ordinal number suffix |
V | shift specified number of digits (see notes) |
EEEE | exponent for scientific notation |
Formatting Functions
Function | Return Type | Description | Example |
---|---|---|---|
to_char(timestamp, text) | text | convert time stamp to string | to_char(current_timestamp, 'HH12:MI:SS') |
to_char(interval, text) | text | convert interval to string | to_char(interval '15h 2m 12s', 'HH24:MI:SS') |
to_char(int, text) | text | convert integer to string | to_char(125, '999') |
to_char(double precision, text) | text | convert real/double precision to string | to_char(125.8::real, '999D9') |
to_char(numeric, text) | text | convert numeric to string | to_char(-125.8, '999D99S') |
to_date(text, text) | date | convert string to date | to_date('05 Dec 2000', 'DD Mon YYYY') |
to_number(text, text) | numeric | convert string to numeric | to_number('12,454.8-', '99G999D9S') |
to_timestamp(text, text) | timestamp with time zone | convert string to time stamp | to_timestamp('05 Dec 2000', 'DD Mon YYYY') |
to_timestamp(double precision) | timestamp with time zone | convert Unix epoch to time stamp | to_timestamp(1284352323) |
In a to_char output template string, there are certain patterns that are recognized and replaced with appropriately-formatted.
Bit String Operators
Operator | Description |
---|---|
|| | concatenation |
& | bitwise AND |
| | bitwise OR |
# | bitwise XOR |
~ | bitwise NOT |
<< | bitwise shift left |
>> | bitwise shift right |
Example: concatenation (||) operator
postgres=# SELECT 'w3' || 'resource'; ?column? ------------ w3resource (1 row)
Example: bitwise AND (&) operator
postgres=# SELECT 2 & 15; ?column? ---------- 2 (1 row)
Example: bitwise OR (|) operator
postgres=# SELECT 2 | 5; ?column? ---------- 7 (1 row)
Example: bitwise XOR (#) operator
postgres=# SELECT 2 ^ 15; ?column? ---------- 32768 (1 row))
Example: bitwise NOT (~) operator
postgres=# SELECT ~2; ?column? ---------- -3 (1 row)
Example: bitwise shift left (<<) operator
postgres=# SELECT 2 << 5; ?column? ---------- 64 (1 row)
Example: bitwise shift right (>>) operator
postgres=# SELECT 8 >> 2; ?column? ---------- 2 (1 row)
Previous:Trigonometric Functions
Next: Geometric Functions and Operators
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/PostgreSQL/data-type-formatting-functions.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics