|
1 |
| -<!-- $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.54 2001/02/21 23:15:24 tgl Exp $ --> |
| 1 | +<!-- $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.55 2001/03/15 01:07:51 tgl Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="functions">
|
4 | 4 | <title>Functions and Operators</title>
|
|
1387 | 1387 | provide a powerful set of tools for converting various data types
|
1388 | 1388 | (date/time, integer, floating point, numeric) to formatted strings
|
1389 | 1389 | and for converting from formatted strings to specific data types.
|
1390 |
| - These functions all follow a common calling convention: The first |
| 1390 | + These functions all follow a common calling convention: the first |
1391 | 1391 | argument is the value to be formatted and the second argument is a
|
1392 |
| - template that defines the output format. |
| 1392 | + template that defines the output or input format. |
1393 | 1393 | </para>
|
1394 | 1394 |
|
1395 | 1395 | <para>
|
|
1437 | 1437 | </row>
|
1438 | 1438 | <row>
|
1439 | 1439 | <entry>to_timestamp(text, text)</entry>
|
1440 |
| - <entry>date</entry> |
| 1440 | + <entry>timestamp</entry> |
1441 | 1441 | <entry>convert string to timestamp</entry>
|
1442 | 1442 | <entry>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</entry>
|
1443 | 1443 | </row>
|
|
1452 | 1452 | </table>
|
1453 | 1453 | </para>
|
1454 | 1454 |
|
| 1455 | + <para> |
| 1456 | + In an output template string, there are certain patterns that are |
| 1457 | + recognized and replaced with appropriately-formatted data from the value |
| 1458 | + to be formatted. Any text that is not a template pattern is simply |
| 1459 | + copied verbatim. Similarly, in an input template string template patterns |
| 1460 | + identify the parts of the input data string to be looked at and the |
| 1461 | + values to be found there. |
| 1462 | + </para> |
| 1463 | + |
1455 | 1464 | <para>
|
1456 | 1465 | <table tocentry="1">
|
1457 |
| - <title>Templates for date/time conversions</title> |
| 1466 | + <title>Template patterns for date/time conversions</title> |
1458 | 1467 | <tgroup cols="2">
|
1459 | 1468 | <thead>
|
1460 | 1469 | <row>
|
1461 |
| - <entry>Template</entry> |
| 1470 | + <entry>Pattern</entry> |
1462 | 1471 | <entry>Description</entry>
|
1463 | 1472 | </row>
|
1464 | 1473 | </thead>
|
|
1525 | 1534 | </row>
|
1526 | 1535 | <row>
|
1527 | 1536 | <entry>MONTH</entry>
|
1528 |
| - <entry>full upper case month name (9 chars)</entry> |
| 1537 | + <entry>full upper case month name (blank-padded to 9 chars)</entry> |
1529 | 1538 | </row>
|
1530 | 1539 | <row>
|
1531 | 1540 | <entry>Month</entry>
|
1532 |
| - <entry>full mixed case month name (9 chars)</entry> |
| 1541 | + <entry>full mixed case month name (blank-padded to 9 chars)</entry> |
1533 | 1542 | </row>
|
1534 | 1543 | <row>
|
1535 | 1544 | <entry>month</entry>
|
1536 |
| - <entry>full lower case month name (9 chars)</entry> |
| 1545 | + <entry>full lower case month name (blank-padded to 9 chars)</entry> |
1537 | 1546 | </row>
|
1538 | 1547 | <row>
|
1539 | 1548 | <entry>MON</entry>
|
1540 |
| - <entry>upper case abbreviated month name (3 chars)</entry> |
| 1549 | + <entry>abbreviated upper case month name (3 chars)</entry> |
1541 | 1550 | </row>
|
1542 | 1551 | <row>
|
1543 | 1552 | <entry>Mon</entry>
|
|
1549 | 1558 | </row>
|
1550 | 1559 | <row>
|
1551 | 1560 | <entry>MM</entry>
|
1552 |
| - <entry>month (01-12)</entry> |
| 1561 | + <entry>month number (01-12)</entry> |
1553 | 1562 | </row>
|
1554 | 1563 | <row>
|
1555 | 1564 | <entry>DAY</entry>
|
1556 |
| - <entry>full upper case day name (9 chars)</entry> |
| 1565 | + <entry>full upper case day name (blank-padded to 9 chars)</entry> |
1557 | 1566 | </row>
|
1558 | 1567 | <row>
|
1559 | 1568 | <entry>Day</entry>
|
1560 |
| - <entry>full mixed case day name (9 chars)</entry> |
| 1569 | + <entry>full mixed case day name (blank-padded to 9 chars)</entry> |
1561 | 1570 | </row>
|
1562 | 1571 | <row>
|
1563 | 1572 | <entry>day</entry>
|
1564 |
| - <entry>full lower case day name (9 chars)</entry> |
| 1573 | + <entry>full lower case day name (blank-padded to 9 chars)</entry> |
1565 | 1574 | </row>
|
1566 | 1575 | <row>
|
1567 | 1576 | <entry>DY</entry>
|
|
1621 | 1630 | </row>
|
1622 | 1631 | <row>
|
1623 | 1632 | <entry>TZ</entry>
|
1624 |
| - <entry>timezone string - upper case</entry> |
| 1633 | + <entry>timezone name - upper case</entry> |
1625 | 1634 | </row>
|
1626 | 1635 | <row>
|
1627 | 1636 | <entry>tz</entry>
|
1628 |
| - <entry>timezone string - lower case</entry> |
| 1637 | + <entry>timezone name - lower case</entry> |
1629 | 1638 | </row>
|
1630 | 1639 | </tbody>
|
1631 | 1640 | </tgroup>
|
1632 | 1641 | </table>
|
1633 | 1642 | </para>
|
1634 | 1643 |
|
1635 | 1644 | <para>
|
1636 |
| - All templates allow the use of prefix and suffix modifiers. Modifiers are |
1637 |
| - always valid for use in templates. The prefix |
1638 |
| - <quote><literal>FX</literal></quote> is a global modifier only. |
| 1645 | + Certain modifiers may be applied to any template pattern to alter its |
| 1646 | + behavior. For example, <quote><literal>FMMonth</literal></quote> |
| 1647 | + is the <quote><literal>Month</literal></quote> pattern with the |
| 1648 | + <quote><literal>FM</literal></quote> prefix. |
1639 | 1649 | </para>
|
1640 | 1650 |
|
1641 | 1651 | <para>
|
1642 | 1652 | <table tocentry="1">
|
1643 |
| - <title>Suffixes for templates for date/time to_char()</title> |
| 1653 | + <title>Template pattern modifiers for date/time conversions</title> |
1644 | 1654 | <tgroup cols="3">
|
1645 | 1655 | <thead>
|
1646 | 1656 | <row>
|
1647 |
| - <entry>Suffix</entry> |
| 1657 | + <entry>Modifier</entry> |
1648 | 1658 | <entry>Description</entry>
|
1649 | 1659 | <entry>Example</entry>
|
1650 | 1660 | </row>
|
1651 | 1661 | </thead>
|
1652 | 1662 | <tbody>
|
1653 | 1663 | <row>
|
1654 |
| - <entry>FM</entry> |
1655 |
| - <entry>fill mode prefix</entry> |
| 1664 | + <entry><literal>FM</literal> prefix</entry> |
| 1665 | + <entry>fill mode (suppress padding blanks and zeroes)</entry> |
1656 | 1666 | <entry>FMMonth</entry>
|
1657 | 1667 | </row>
|
1658 | 1668 | <row>
|
1659 |
| - <entry>TH</entry> |
1660 |
| - <entry>upper ordinal number suffix</entry> |
| 1669 | + <entry><literal>TH</literal> suffix</entry> |
| 1670 | + <entry>add upper-case ordinal number suffix</entry> |
1661 | 1671 | <entry>DDTH</entry>
|
1662 | 1672 | </row>
|
1663 | 1673 | <row>
|
1664 |
| - <entry>th</entry> |
1665 |
| - <entry>lower ordinal number suffix</entry> |
1666 |
| - <entry>DDTH</entry> |
| 1674 | + <entry><literal>th</literal> suffix</entry> |
| 1675 | + <entry>add lower-case ordinal number suffix</entry> |
| 1676 | + <entry>DDth</entry> |
1667 | 1677 | </row>
|
1668 | 1678 | <row>
|
1669 |
| - <entry>FX</entry> |
| 1679 | + <entry><literal>FX</literal> prefix</entry> |
1670 | 1680 | <entry>FiXed format global option (see below)</entry>
|
1671 | 1681 | <entry>FX Month DD Day</entry>
|
1672 | 1682 | </row>
|
1673 | 1683 | <row>
|
1674 |
| - <entry>SP</entry> |
| 1684 | + <entry><literal>SP</literal> suffix</entry> |
1675 | 1685 | <entry>spell mode (not yet implemented)</entry>
|
1676 | 1686 | <entry>DDSP</entry>
|
1677 | 1687 | </row>
|
|
1684 | 1694 | Usage notes:
|
1685 | 1695 |
|
1686 | 1696 | <itemizedlist>
|
| 1697 | + <listitem> |
| 1698 | + <para> |
| 1699 | + <literal>FM</literal> suppresses leading zeroes or trailing blanks |
| 1700 | + that would otherwise be added to make the output of a pattern be |
| 1701 | + fixed-width. |
| 1702 | + </para> |
| 1703 | + </listitem> |
| 1704 | + |
1687 | 1705 | <listitem>
|
1688 | 1706 | <para>
|
1689 | 1707 | <function>to_timestamp</function> and <function>to_date</function>
|
1690 |
| - skip multiple blank space in converted string if the <literal>FX</literal> option |
| 1708 | + skip multiple blank spaces in the input string if the <literal>FX</literal> option |
1691 | 1709 | is not used. <literal>FX</literal> must be specified as the first item
|
1692 | 1710 | in the template; for example
|
1693 | 1711 | <literal>to_timestamp('2000 JUN','YYYY MON')</literal> is right, but
|
1694 |
| - <literal>to_timestamp('2000 JUN','FXYYYY MON')</literal> returns error, |
1695 |
| - because to_timestamp() expects one blank space only. |
| 1712 | + <literal>to_timestamp('2000 JUN','FXYYYY MON')</literal> returns an error, |
| 1713 | + because <function>to_timestamp</function> expects one blank space only. |
1696 | 1714 | </para>
|
1697 | 1715 | </listitem>
|
1698 | 1716 |
|
|
1709 | 1727 | <listitem>
|
1710 | 1728 | <para>
|
1711 | 1729 | Ordinary text is allowed in <function>to_char</function>
|
1712 |
| - templates but any string between double quotes is guaranteed |
1713 |
| - that it will not be interpreted as a template keyword and it is |
1714 |
| - also processed faster. (Example: <literal>'"Hello Year: |
1715 |
| - "YYYY'</literal>). |
| 1730 | + templates and will be output literally. You can put a substring |
| 1731 | + in double quotes to force it to be interpreted as literal text |
| 1732 | + even if it contains pattern keywords. For example, in |
| 1733 | + <literal>'"Hello Year: "YYYY'</literal>, the <literal>YYYY</literal> |
| 1734 | + will be replaced by year data, but the single <literal>Y</literal> |
| 1735 | + will not be. |
1716 | 1736 | </para>
|
1717 | 1737 | </listitem>
|
1718 | 1738 |
|
1719 | 1739 | <listitem>
|
1720 | 1740 | <para>
|
1721 |
| - A double quote (<quote><literal>"</literal></quote>) between |
1722 |
| - quotation marks is skipped and is not parsed. If you want to |
| 1741 | + If you want to |
1723 | 1742 | have a double quote in the output you must precede it with a
|
1724 |
| - double backslash, for example <literal>'\\"YYYY |
| 1743 | + backslash, for example <literal>'\\"YYYY |
1725 | 1744 | Month\\"'</literal>. <!-- " font-lock sanity :-) -->
|
1726 | 1745 | </para>
|
1727 | 1746 | </listitem>
|
|
1745 | 1764 |
|
1746 | 1765 | <para>
|
1747 | 1766 | <table tocentry="1">
|
1748 |
| - <title>Templates for to_char(<replaceable>numeric</replaceable>)</title> |
| 1767 | + <title>Template patterns for numeric conversions</title> |
1749 | 1768 | <tgroup cols="2">
|
1750 | 1769 | <thead>
|
1751 | 1770 | <row>
|
1752 |
| - <entry>Template</entry> |
| 1771 | + <entry>Pattern</entry> |
1753 | 1772 | <entry>Description</entry>
|
1754 | 1773 | </row>
|
1755 | 1774 | </thead>
|
|
1843 | 1862 |
|
1844 | 1863 | <listitem>
|
1845 | 1864 | <para>
|
1846 |
| - <literal>PL</literal>, <literal>SG</literal>, and |
1847 |
| - <literal>TH</literal> are <productname>Postgres</productname> |
1848 |
| - extensions. |
| 1865 | + <literal>9</literal> specifies a value with the same number of |
| 1866 | + digits as there are <literal>9</literal>s. If a digit is |
| 1867 | + not available use blank space. |
1849 | 1868 | </para>
|
1850 | 1869 | </listitem>
|
1851 | 1870 |
|
1852 | 1871 | <listitem>
|
1853 | 1872 | <para>
|
1854 |
| - <literal>9</literal> specifies a value with the same number of |
1855 |
| - digits as there are <literal>9</literal>s. If a digit is |
1856 |
| - not available use blank space. |
| 1873 | + <literal>TH</literal> does not convert values less than zero |
| 1874 | + and does not convert decimal numbers. |
1857 | 1875 | </para>
|
1858 | 1876 | </listitem>
|
1859 | 1877 |
|
1860 | 1878 | <listitem>
|
1861 | 1879 | <para>
|
1862 |
| - <literal>TH</literal> does not convert values less than zero |
1863 |
| - and does not convert decimal numbers. <literal>TH</literal> is |
1864 |
| - a <productname>Postgres</productname> extension. |
| 1880 | + <literal>PL</literal>, <literal>SG</literal>, and |
| 1881 | + <literal>TH</literal> are <productname>Postgres</productname> |
| 1882 | + extensions. |
1865 | 1883 | </para>
|
1866 | 1884 | </listitem>
|
1867 | 1885 |
|
|
1892 | 1910 | </thead>
|
1893 | 1911 | <tbody>
|
1894 | 1912 | <row>
|
1895 |
| - <entry>to_char(now(),'Day, HH12:MI:SS')</entry> |
1896 |
| - <entry><literal>'Tuesday , 05:39:18'</literal></entry> |
| 1913 | + <entry>to_char(now(),'Day, DD HH12:MI:SS')</entry> |
| 1914 | + <entry><literal>'Tuesday , 06 05:39:18'</literal></entry> |
1897 | 1915 | </row>
|
1898 | 1916 | <row>
|
1899 |
| - <entry>to_char(now(),'FMDay, HH12:MI:SS')</entry> |
1900 |
| - <entry><literal>'Tuesday, 05:39:18'</literal></entry> |
| 1917 | + <entry>to_char(now(),'FMDay, FMDD HH12:MI:SS')</entry> |
| 1918 | + <entry><literal>'Tuesday, 6 05:39:18'</literal></entry> |
1901 | 1919 | </row>
|
1902 | 1920 | <row>
|
1903 | 1921 | <entry>to_char(-0.1,'99.99')</entry>
|
|
0 commit comments