gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
41KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.8. Data Type Formatting Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="functions-matching.html" title="9.7. Pattern Matching" /><link rel="next" href="functions-datetime.html" title="9.9. Date/Time Functions and Operators" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.8. Data Type Formatting Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-matching.html" title="9.7. Pattern Matching">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-datetime.html" title="9.9. Date/Time Functions and Operators">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-FORMATTING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.8. Data Type Formatting Functions</h2></div></div></div><a id="id-1.5.8.13.2" class="indexterm"></a><p>
  3. The <span class="productname">PostgreSQL</span> formatting functions
  4. provide a powerful set of tools for converting various data types
  5. (date/time, integer, floating point, numeric) to formatted strings
  6. and for converting from formatted strings to specific data types.
  7. <a class="xref" href="functions-formatting.html#FUNCTIONS-FORMATTING-TABLE" title="Table 9.24. Formatting Functions">Table 9.24</a> lists them.
  8. These functions all follow a common calling convention: the first
  9. argument is the value to be formatted and the second argument is a
  10. template that defines the output or input format.
  11. </p><div class="table" id="FUNCTIONS-FORMATTING-TABLE"><p class="title"><strong>Table 9.24. Formatting Functions</strong></p><div class="table-contents"><table class="table" summary="Formatting Functions" border="1"><colgroup><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Return Type</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>
  12. <a id="id-1.5.8.13.4.2.2.1.1.1" class="indexterm"></a>
  13. <code class="literal"><code class="function">to_char(<code class="type">timestamp</code>, <code class="type">text</code>)</code></code>
  14. </td><td><code class="type">text</code></td><td>convert time stamp to string</td><td><code class="literal">to_char(current_timestamp, 'HH12:MI:SS')</code></td></tr><tr><td><code class="literal"><code class="function">to_char(<code class="type">interval</code>, <code class="type">text</code>)</code></code></td><td><code class="type">text</code></td><td>convert interval to string</td><td><code class="literal">to_char(interval '15h 2m 12s', 'HH24:MI:SS')</code></td></tr><tr><td><code class="literal"><code class="function">to_char(<code class="type">int</code>, <code class="type">text</code>)</code></code></td><td><code class="type">text</code></td><td>convert integer to string</td><td><code class="literal">to_char(125, '999')</code></td></tr><tr><td><code class="literal"><code class="function">to_char</code>(<code class="type">double precision</code>,
  15. <code class="type">text</code>)</code></td><td><code class="type">text</code></td><td>convert real/double precision to string</td><td><code class="literal">to_char(125.8::real, '999D9')</code></td></tr><tr><td><code class="literal"><code class="function">to_char(<code class="type">numeric</code>, <code class="type">text</code>)</code></code></td><td><code class="type">text</code></td><td>convert numeric to string</td><td><code class="literal">to_char(-125.8, '999D99S')</code></td></tr><tr><td>
  16. <a id="id-1.5.8.13.4.2.2.6.1.1" class="indexterm"></a>
  17. <code class="literal"><code class="function">to_date(<code class="type">text</code>, <code class="type">text</code>)</code></code>
  18. </td><td><code class="type">date</code></td><td>convert string to date</td><td><code class="literal">to_date('05 Dec 2000', 'DD Mon YYYY')</code></td></tr><tr><td>
  19. <a id="id-1.5.8.13.4.2.2.7.1.1" class="indexterm"></a>
  20. <code class="literal"><code class="function">to_number(<code class="type">text</code>, <code class="type">text</code>)</code></code>
  21. </td><td><code class="type">numeric</code></td><td>convert string to numeric</td><td><code class="literal">to_number('12,454.8-', '99G999D9S')</code></td></tr><tr><td>
  22. <a id="id-1.5.8.13.4.2.2.8.1.1" class="indexterm"></a>
  23. <code class="literal"><code class="function">to_timestamp(<code class="type">text</code>, <code class="type">text</code>)</code></code>
  24. </td><td><code class="type">timestamp with time zone</code></td><td>convert string to time stamp</td><td><code class="literal">to_timestamp('05 Dec 2000', 'DD Mon YYYY')</code></td></tr></tbody></table></div></div><br class="table-break" /><div class="note"><h3 class="title">Note</h3><p>
  25. There is also a single-argument <code class="function">to_timestamp</code>
  26. function; see <a class="xref" href="functions-datetime.html#FUNCTIONS-DATETIME-TABLE" title="Table 9.31. Date/Time Functions">Table 9.31</a>.
  27. </p></div><div class="tip"><h3 class="title">Tip</h3><p>
  28. <code class="function">to_timestamp</code> and <code class="function">to_date</code>
  29. exist to handle input formats that cannot be converted by
  30. simple casting. For most standard date/time formats, simply casting the
  31. source string to the required data type works, and is much easier.
  32. Similarly, <code class="function">to_number</code> is unnecessary for standard numeric
  33. representations.
  34. </p></div><p>
  35. In a <code class="function">to_char</code> output template string, there are certain
  36. patterns that are recognized and replaced with appropriately-formatted
  37. data based on the given value. Any text that is not a template pattern is
  38. simply copied verbatim. Similarly, in an input template string (for the
  39. other functions), template patterns identify the values to be supplied by
  40. the input data string. If there are characters in the template string
  41. that are not template patterns, the corresponding characters in the input
  42. data string are simply skipped over (whether or not they are equal to the
  43. template string characters).
  44. </p><p>
  45. <a class="xref" href="functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE" title="Table 9.25. Template Patterns for Date/Time Formatting">Table 9.25</a> shows the
  46. template patterns available for formatting date and time values.
  47. </p><div class="table" id="FUNCTIONS-FORMATTING-DATETIME-TABLE"><p class="title"><strong>Table 9.25. Template Patterns for Date/Time Formatting</strong></p><div class="table-contents"><table class="table" summary="Template Patterns for Date/Time Formatting" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Pattern</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">HH</code></td><td>hour of day (01-12)</td></tr><tr><td><code class="literal">HH12</code></td><td>hour of day (01-12)</td></tr><tr><td><code class="literal">HH24</code></td><td>hour of day (00-23)</td></tr><tr><td><code class="literal">MI</code></td><td>minute (00-59)</td></tr><tr><td><code class="literal">SS</code></td><td>second (00-59)</td></tr><tr><td><code class="literal">MS</code></td><td>millisecond (000-999)</td></tr><tr><td><code class="literal">US</code></td><td>microsecond (000000-999999)</td></tr><tr><td><code class="literal">SSSS</code></td><td>seconds past midnight (0-86399)</td></tr><tr><td><code class="literal">AM</code>, <code class="literal">am</code>,
  48. <code class="literal">PM</code> or <code class="literal">pm</code></td><td>meridiem indicator (without periods)</td></tr><tr><td><code class="literal">A.M.</code>, <code class="literal">a.m.</code>,
  49. <code class="literal">P.M.</code> or <code class="literal">p.m.</code></td><td>meridiem indicator (with periods)</td></tr><tr><td><code class="literal">Y,YYY</code></td><td>year (4 or more digits) with comma</td></tr><tr><td><code class="literal">YYYY</code></td><td>year (4 or more digits)</td></tr><tr><td><code class="literal">YYY</code></td><td>last 3 digits of year</td></tr><tr><td><code class="literal">YY</code></td><td>last 2 digits of year</td></tr><tr><td><code class="literal">Y</code></td><td>last digit of year</td></tr><tr><td><code class="literal">IYYY</code></td><td>ISO 8601 week-numbering year (4 or more digits)</td></tr><tr><td><code class="literal">IYY</code></td><td>last 3 digits of ISO 8601 week-numbering year</td></tr><tr><td><code class="literal">IY</code></td><td>last 2 digits of ISO 8601 week-numbering year</td></tr><tr><td><code class="literal">I</code></td><td>last digit of ISO 8601 week-numbering year</td></tr><tr><td><code class="literal">BC</code>, <code class="literal">bc</code>,
  50. <code class="literal">AD</code> or <code class="literal">ad</code></td><td>era indicator (without periods)</td></tr><tr><td><code class="literal">B.C.</code>, <code class="literal">b.c.</code>,
  51. <code class="literal">A.D.</code> or <code class="literal">a.d.</code></td><td>era indicator (with periods)</td></tr><tr><td><code class="literal">MONTH</code></td><td>full upper case month name (blank-padded to 9 chars)</td></tr><tr><td><code class="literal">Month</code></td><td>full capitalized month name (blank-padded to 9 chars)</td></tr><tr><td><code class="literal">month</code></td><td>full lower case month name (blank-padded to 9 chars)</td></tr><tr><td><code class="literal">MON</code></td><td>abbreviated upper case month name (3 chars in English, localized lengths vary)</td></tr><tr><td><code class="literal">Mon</code></td><td>abbreviated capitalized month name (3 chars in English, localized lengths vary)</td></tr><tr><td><code class="literal">mon</code></td><td>abbreviated lower case month name (3 chars in English, localized lengths vary)</td></tr><tr><td><code class="literal">MM</code></td><td>month number (01-12)</td></tr><tr><td><code class="literal">DAY</code></td><td>full upper case day name (blank-padded to 9 chars)</td></tr><tr><td><code class="literal">Day</code></td><td>full capitalized day name (blank-padded to 9 chars)</td></tr><tr><td><code class="literal">day</code></td><td>full lower case day name (blank-padded to 9 chars)</td></tr><tr><td><code class="literal">DY</code></td><td>abbreviated upper case day name (3 chars in English, localized lengths vary)</td></tr><tr><td><code class="literal">Dy</code></td><td>abbreviated capitalized day name (3 chars in English, localized lengths vary)</td></tr><tr><td><code class="literal">dy</code></td><td>abbreviated lower case day name (3 chars in English, localized lengths vary)</td></tr><tr><td><code class="literal">DDD</code></td><td>day of year (001-366)</td></tr><tr><td><code class="literal">IDDD</code></td><td>day of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday of the first ISO week)</td></tr><tr><td><code class="literal">DD</code></td><td>day of month (01-31)</td></tr><tr><td><code class="literal">D</code></td><td>day of the week, Sunday (<code class="literal">1</code>) to Saturday (<code class="literal">7</code>)</td></tr><tr><td><code class="literal">ID</code></td><td>ISO 8601 day of the week, Monday (<code class="literal">1</code>) to Sunday (<code class="literal">7</code>)</td></tr><tr><td><code class="literal">W</code></td><td>week of month (1-5) (the first week starts on the first day of the month)</td></tr><tr><td><code class="literal">WW</code></td><td>week number of year (1-53) (the first week starts on the first day of the year)</td></tr><tr><td><code class="literal">IW</code></td><td>week number of ISO 8601 week-numbering year (01-53; the first Thursday of the year is in week 1)</td></tr><tr><td><code class="literal">CC</code></td><td>century (2 digits) (the twenty-first century starts on 2001-01-01)</td></tr><tr><td><code class="literal">J</code></td><td>Julian Day (integer days since November 24, 4714 BC at midnight UTC)</td></tr><tr><td><code class="literal">Q</code></td><td>quarter</td></tr><tr><td><code class="literal">RM</code></td><td>month in upper case Roman numerals (I-XII; I=January)</td></tr><tr><td><code class="literal">rm</code></td><td>month in lower case Roman numerals (i-xii; i=January)</td></tr><tr><td><code class="literal">TZ</code></td><td>upper case time-zone abbreviation
  52. (only supported in <code class="function">to_char</code>)</td></tr><tr><td><code class="literal">tz</code></td><td>lower case time-zone abbreviation
  53. (only supported in <code class="function">to_char</code>)</td></tr><tr><td><code class="literal">TZH</code></td><td>time-zone hours</td></tr><tr><td><code class="literal">TZM</code></td><td>time-zone minutes</td></tr><tr><td><code class="literal">OF</code></td><td>time-zone offset from UTC
  54. (only supported in <code class="function">to_char</code>)</td></tr></tbody></table></div></div><br class="table-break" /><p>
  55. Modifiers can be applied to any template pattern to alter its
  56. behavior. For example, <code class="literal">FMMonth</code>
  57. is the <code class="literal">Month</code> pattern with the
  58. <code class="literal">FM</code> modifier.
  59. <a class="xref" href="functions-formatting.html#FUNCTIONS-FORMATTING-DATETIMEMOD-TABLE" title="Table 9.26. Template Pattern Modifiers for Date/Time Formatting">Table 9.26</a> shows the
  60. modifier patterns for date/time formatting.
  61. </p><div class="table" id="FUNCTIONS-FORMATTING-DATETIMEMOD-TABLE"><p class="title"><strong>Table 9.26. Template Pattern Modifiers for Date/Time Formatting</strong></p><div class="table-contents"><table class="table" summary="Template Pattern Modifiers for Date/Time Formatting" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Modifier</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td><code class="literal">FM</code> prefix</td><td>fill mode (suppress leading zeroes and padding blanks)</td><td><code class="literal">FMMonth</code></td></tr><tr><td><code class="literal">TH</code> suffix</td><td>upper case ordinal number suffix</td><td><code class="literal">DDTH</code>, e.g., <code class="literal">12TH</code></td></tr><tr><td><code class="literal">th</code> suffix</td><td>lower case ordinal number suffix</td><td><code class="literal">DDth</code>, e.g., <code class="literal">12th</code></td></tr><tr><td><code class="literal">FX</code> prefix</td><td>fixed format global option (see usage notes)</td><td><code class="literal">FX Month DD Day</code></td></tr><tr><td><code class="literal">TM</code> prefix</td><td>translation mode (print localized day and month names based on
  62. <a class="xref" href="runtime-config-client.html#GUC-LC-TIME">lc_time</a>)</td><td><code class="literal">TMMonth</code></td></tr><tr><td><code class="literal">SP</code> suffix</td><td>spell mode (not implemented)</td><td><code class="literal">DDSP</code></td></tr></tbody></table></div></div><br class="table-break" /><p>
  63. Usage notes for date/time formatting:
  64. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  65. <code class="literal">FM</code> suppresses leading zeroes and trailing blanks
  66. that would otherwise be added to make the output of a pattern be
  67. fixed-width. In <span class="productname">PostgreSQL</span>,
  68. <code class="literal">FM</code> modifies only the next specification, while in
  69. Oracle <code class="literal">FM</code> affects all subsequent
  70. specifications, and repeated <code class="literal">FM</code> modifiers
  71. toggle fill mode on and off.
  72. </p></li><li class="listitem"><p>
  73. <code class="literal">TM</code> does not include trailing blanks.
  74. <code class="function">to_timestamp</code> and <code class="function">to_date</code> ignore
  75. the <code class="literal">TM</code> modifier.
  76. </p></li><li class="listitem"><p>
  77. <code class="function">to_timestamp</code> and <code class="function">to_date</code>
  78. skip multiple blank spaces at the beginning of the input string and
  79. around date and time values unless the <code class="literal">FX</code> option is used. For example,
  80. <code class="literal">to_timestamp(' 2000    JUN', 'YYYY MON')</code> and
  81. <code class="literal">to_timestamp('2000 - JUN', 'YYYY-MON')</code> work, but
  82. <code class="literal">to_timestamp('2000    JUN', 'FXYYYY MON')</code> returns an error
  83. because <code class="function">to_timestamp</code> expects only a single space.
  84. <code class="literal">FX</code> must be specified as the first item in
  85. the template.
  86. </p></li><li class="listitem"><p>
  87. A separator (a space or non-letter/non-digit character) in the template string of
  88. <code class="function">to_timestamp</code> and <code class="function">to_date</code>
  89. matches any single separator in the input string or is skipped,
  90. unless the <code class="literal">FX</code> option is used.
  91. For example, <code class="literal">to_timestamp('2000JUN', 'YYYY///MON')</code> and
  92. <code class="literal">to_timestamp('2000/JUN', 'YYYY MON')</code> work, but
  93. <code class="literal">to_timestamp('2000//JUN', 'YYYY/MON')</code>
  94. returns an error because the number of separators in the input string
  95. exceeds the number of separators in the template.
  96. </p><p>
  97. If <code class="literal">FX</code> is specified, a separator in the template string
  98. matches exactly one character in the input string. But note that the
  99. input string character is not required to be the same as the separator from the template string.
  100. For example, <code class="literal">to_timestamp('2000/JUN', 'FXYYYY MON')</code>
  101. works, but <code class="literal">to_timestamp('2000/JUN', 'FXYYYY  MON')</code>
  102. returns an error because the second space in the template string consumes
  103. the letter <code class="literal">J</code> from the input string.
  104. </p></li><li class="listitem"><p>
  105. A <code class="literal">TZH</code> template pattern can match a signed number.
  106. Without the <code class="literal">FX</code> option, minus signs may be ambiguous,
  107. and could be interpreted as a separator.
  108. This ambiguity is resolved as follows: If the number of separators before
  109. <code class="literal">TZH</code> in the template string is less than the number of
  110. separators before the minus sign in the input string, the minus sign
  111. is interpreted as part of <code class="literal">TZH</code>.
  112. Otherwise, the minus sign is considered to be a separator between values.
  113. For example, <code class="literal">to_timestamp('2000 -10', 'YYYY TZH')</code> matches
  114. <code class="literal">-10</code> to <code class="literal">TZH</code>, but
  115. <code class="literal">to_timestamp('2000 -10', 'YYYY  TZH')</code>
  116. matches <code class="literal">10</code> to <code class="literal">TZH</code>.
  117. </p></li><li class="listitem"><p>
  118. Ordinary text is allowed in <code class="function">to_char</code>
  119. templates and will be output literally. You can put a substring
  120. in double quotes to force it to be interpreted as literal text
  121. even if it contains template patterns. For example, in
  122. <code class="literal">'"Hello Year "YYYY'</code>, the <code class="literal">YYYY</code>
  123. will be replaced by the year data, but the single <code class="literal">Y</code> in <code class="literal">Year</code>
  124. will not be.
  125. In <code class="function">to_date</code>, <code class="function">to_number</code>,
  126. and <code class="function">to_timestamp</code>, literal text and double-quoted
  127. strings result in skipping the number of characters contained in the
  128. string; for example <code class="literal">"XX"</code> skips two input characters
  129. (whether or not they are <code class="literal">XX</code>).
  130. </p><div class="tip"><h3 class="title">Tip</h3><p>
  131. Prior to <span class="productname">PostgreSQL</span> 12, it was possible to
  132. skip arbitrary text in the input string using non-letter or non-digit
  133. characters. For example,
  134. <code class="literal">to_timestamp('2000y6m1d', 'yyyy-MM-DD')</code> used to
  135. work. Now you can only use letter characters for this purpose. For example,
  136. <code class="literal">to_timestamp('2000y6m1d', 'yyyytMMtDDt')</code> and
  137. <code class="literal">to_timestamp('2000y6m1d', 'yyyy"y"MM"m"DD"d"')</code>
  138. skip <code class="literal">y</code>, <code class="literal">m</code>, and
  139. <code class="literal">d</code>.
  140. </p></div></li><li class="listitem"><p>
  141. If you want to have a double quote in the output you must
  142. precede it with a backslash, for example <code class="literal">'\"YYYY
  143. Month\"'</code>.
  144. Backslashes are not otherwise special outside of double-quoted
  145. strings. Within a double-quoted string, a backslash causes the
  146. next character to be taken literally, whatever it is (but this
  147. has no special effect unless the next character is a double quote
  148. or another backslash).
  149. </p></li><li class="listitem"><p>
  150. In <code class="function">to_timestamp</code> and <code class="function">to_date</code>,
  151. if the year format specification is less than four digits, e.g.
  152. <code class="literal">YYY</code>, and the supplied year is less than four digits,
  153. the year will be adjusted to be nearest to the year 2020, e.g.
  154. <code class="literal">95</code> becomes 1995.
  155. </p></li><li class="listitem"><p>
  156. In <code class="function">to_timestamp</code> and <code class="function">to_date</code>,
  157. the <code class="literal">YYYY</code> conversion has a restriction when
  158. processing years with more than 4 digits. You must
  159. use some non-digit character or template after <code class="literal">YYYY</code>,
  160. otherwise the year is always interpreted as 4 digits. For example
  161. (with the year 20000):
  162. <code class="literal">to_date('200001131', 'YYYYMMDD')</code> will be
  163. interpreted as a 4-digit year; instead use a non-digit
  164. separator after the year, like
  165. <code class="literal">to_date('20000-1131', 'YYYY-MMDD')</code> or
  166. <code class="literal">to_date('20000Nov31', 'YYYYMonDD')</code>.
  167. </p></li><li class="listitem"><p>
  168. In <code class="function">to_timestamp</code> and <code class="function">to_date</code>,
  169. the <code class="literal">CC</code> (century) field is accepted but ignored
  170. if there is a <code class="literal">YYY</code>, <code class="literal">YYYY</code> or
  171. <code class="literal">Y,YYY</code> field. If <code class="literal">CC</code> is used with
  172. <code class="literal">YY</code> or <code class="literal">Y</code> then the result is
  173. computed as that year in the specified century. If the century is
  174. specified but the year is not, the first year of the century
  175. is assumed.
  176. </p></li><li class="listitem"><p>
  177. In <code class="function">to_timestamp</code> and <code class="function">to_date</code>,
  178. weekday names or numbers (<code class="literal">DAY</code>, <code class="literal">D</code>,
  179. and related field types) are accepted but are ignored for purposes of
  180. computing the result. The same is true for quarter
  181. (<code class="literal">Q</code>) fields.
  182. </p></li><li class="listitem"><p>
  183. In <code class="function">to_timestamp</code> and <code class="function">to_date</code>,
  184. an ISO 8601 week-numbering date (as distinct from a Gregorian date)
  185. can be specified in one of two ways:
  186. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  187. Year, week number, and weekday: for
  188. example <code class="literal">to_date('2006-42-4', 'IYYY-IW-ID')</code>
  189. returns the date <code class="literal">2006-10-19</code>.
  190. If you omit the weekday it is assumed to be 1 (Monday).
  191. </p></li><li class="listitem"><p>
  192. Year and day of year: for example <code class="literal">to_date('2006-291',
  193. 'IYYY-IDDD')</code> also returns <code class="literal">2006-10-19</code>.
  194. </p></li></ul></div><p>
  195. </p><p>
  196. Attempting to enter a date using a mixture of ISO 8601 week-numbering
  197. fields and Gregorian date fields is nonsensical, and will cause an
  198. error. In the context of an ISO 8601 week-numbering year, the
  199. concept of a <span class="quote">“<span class="quote">month</span>”</span> or <span class="quote">“<span class="quote">day of month</span>”</span> has no
  200. meaning. In the context of a Gregorian year, the ISO week has no
  201. meaning.
  202. </p><div class="caution"><h3 class="title">Caution</h3><p>
  203. While <code class="function">to_date</code> will reject a mixture of
  204. Gregorian and ISO week-numbering date
  205. fields, <code class="function">to_char</code> will not, since output format
  206. specifications like <code class="literal">YYYY-MM-DD (IYYY-IDDD)</code> can be
  207. useful. But avoid writing something like <code class="literal">IYYY-MM-DD</code>;
  208. that would yield surprising results near the start of the year.
  209. (See <a class="xref" href="functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT" title="9.9.1. EXTRACT, date_part">Section 9.9.1</a> for more
  210. information.)
  211. </p></div></li><li class="listitem"><p>
  212. In <code class="function">to_timestamp</code>, millisecond
  213. (<code class="literal">MS</code>) or microsecond (<code class="literal">US</code>)
  214. fields are used as the
  215. seconds digits after the decimal point. For example
  216. <code class="literal">to_timestamp('12.3', 'SS.MS')</code> is not 3 milliseconds,
  217. but 300, because the conversion treats it as 12 + 0.3 seconds.
  218. So, for the format <code class="literal">SS.MS</code>, the input values
  219. <code class="literal">12.3</code>, <code class="literal">12.30</code>,
  220. and <code class="literal">12.300</code> specify the
  221. same number of milliseconds. To get three milliseconds, one must write
  222. <code class="literal">12.003</code>, which the conversion treats as
  223. 12 + 0.003 = 12.003 seconds.
  224. </p><p>
  225. Here is a more
  226. complex example:
  227. <code class="literal">to_timestamp('15:12:02.020.001230', 'HH24:MI:SS.MS.US')</code>
  228. is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
  229. 1230 microseconds = 2.021230 seconds.
  230. </p></li><li class="listitem"><p>
  231. <code class="function">to_char(..., 'ID')</code>'s day of the week numbering
  232. matches the <code class="function">extract(isodow from ...)</code> function, but
  233. <code class="function">to_char(..., 'D')</code>'s does not match
  234. <code class="function">extract(dow from ...)</code>'s day numbering.
  235. </p></li><li class="listitem"><p>
  236. <code class="function">to_char(interval)</code> formats <code class="literal">HH</code> and
  237. <code class="literal">HH12</code> as shown on a 12-hour clock, for example zero hours
  238. and 36 hours both output as <code class="literal">12</code>, while <code class="literal">HH24</code>
  239. outputs the full hour value, which can exceed 23 in
  240. an <code class="type">interval</code> value.
  241. </p></li></ul></div><p>
  242. </p><p>
  243. <a class="xref" href="functions-formatting.html#FUNCTIONS-FORMATTING-NUMERIC-TABLE" title="Table 9.27. Template Patterns for Numeric Formatting">Table 9.27</a> shows the
  244. template patterns available for formatting numeric values.
  245. </p><div class="table" id="FUNCTIONS-FORMATTING-NUMERIC-TABLE"><p class="title"><strong>Table 9.27. Template Patterns for Numeric Formatting</strong></p><div class="table-contents"><table class="table" summary="Template Patterns for Numeric Formatting" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Pattern</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">9</code></td><td>digit position (can be dropped if insignificant)</td></tr><tr><td><code class="literal">0</code></td><td>digit position (will not be dropped, even if insignificant)</td></tr><tr><td><code class="literal">.</code> (period)</td><td>decimal point</td></tr><tr><td><code class="literal">,</code> (comma)</td><td>group (thousands) separator</td></tr><tr><td><code class="literal">PR</code></td><td>negative value in angle brackets</td></tr><tr><td><code class="literal">S</code></td><td>sign anchored to number (uses locale)</td></tr><tr><td><code class="literal">L</code></td><td>currency symbol (uses locale)</td></tr><tr><td><code class="literal">D</code></td><td>decimal point (uses locale)</td></tr><tr><td><code class="literal">G</code></td><td>group separator (uses locale)</td></tr><tr><td><code class="literal">MI</code></td><td>minus sign in specified position (if number &lt; 0)</td></tr><tr><td><code class="literal">PL</code></td><td>plus sign in specified position (if number &gt; 0)</td></tr><tr><td><code class="literal">SG</code></td><td>plus/minus sign in specified position</td></tr><tr><td><code class="literal">RN</code></td><td>Roman numeral (input between 1 and 3999)</td></tr><tr><td><code class="literal">TH</code> or <code class="literal">th</code></td><td>ordinal number suffix</td></tr><tr><td><code class="literal">V</code></td><td>shift specified number of digits (see notes)</td></tr><tr><td><code class="literal">EEEE</code></td><td>exponent for scientific notation</td></tr></tbody></table></div></div><br class="table-break" /><p>
  246. Usage notes for numeric formatting:
  247. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  248. <code class="literal">0</code> specifies a digit position that will always be printed,
  249. even if it contains a leading/trailing zero. <code class="literal">9</code> also
  250. specifies a digit position, but if it is a leading zero then it will
  251. be replaced by a space, while if it is a trailing zero and fill mode
  252. is specified then it will be deleted. (For <code class="function">to_number()</code>,
  253. these two pattern characters are equivalent.)
  254. </p></li><li class="listitem"><p>
  255. The pattern characters <code class="literal">S</code>, <code class="literal">L</code>, <code class="literal">D</code>,
  256. and <code class="literal">G</code> represent the sign, currency symbol, decimal point,
  257. and thousands separator characters defined by the current locale
  258. (see <a class="xref" href="runtime-config-client.html#GUC-LC-MONETARY">lc_monetary</a>
  259. and <a class="xref" href="runtime-config-client.html#GUC-LC-NUMERIC">lc_numeric</a>). The pattern characters period
  260. and comma represent those exact characters, with the meanings of
  261. decimal point and thousands separator, regardless of locale.
  262. </p></li><li class="listitem"><p>
  263. If no explicit provision is made for a sign
  264. in <code class="function">to_char()</code>'s pattern, one column will be reserved for
  265. the sign, and it will be anchored to (appear just left of) the
  266. number. If <code class="literal">S</code> appears just left of some <code class="literal">9</code>'s,
  267. it will likewise be anchored to the number.
  268. </p></li><li class="listitem"><p>
  269. A sign formatted using <code class="literal">SG</code>, <code class="literal">PL</code>, or
  270. <code class="literal">MI</code> is not anchored to
  271. the number; for example,
  272. <code class="literal">to_char(-12, 'MI9999')</code> produces <code class="literal">'-  12'</code>
  273. but <code class="literal">to_char(-12, 'S9999')</code> produces <code class="literal">'  -12'</code>.
  274. (The Oracle implementation does not allow the use of
  275. <code class="literal">MI</code> before <code class="literal">9</code>, but rather
  276. requires that <code class="literal">9</code> precede
  277. <code class="literal">MI</code>.)
  278. </p></li><li class="listitem"><p>
  279. <code class="literal">TH</code> does not convert values less than zero
  280. and does not convert fractional numbers.
  281. </p></li><li class="listitem"><p>
  282. <code class="literal">PL</code>, <code class="literal">SG</code>, and
  283. <code class="literal">TH</code> are <span class="productname">PostgreSQL</span>
  284. extensions.
  285. </p></li><li class="listitem"><p>
  286. In <code class="function">to_number</code>, if non-data template patterns such
  287. as <code class="literal">L</code> or <code class="literal">TH</code> are used, the
  288. corresponding number of input characters are skipped, whether or not
  289. they match the template pattern, unless they are data characters
  290. (that is, digits, sign, decimal point, or comma). For
  291. example, <code class="literal">TH</code> would skip two non-data characters.
  292. </p></li><li class="listitem"><p>
  293. <code class="literal">V</code> with <code class="function">to_char</code>
  294. multiplies the input values by
  295. <code class="literal">10^<em class="replaceable"><code>n</code></em></code>, where
  296. <em class="replaceable"><code>n</code></em> is the number of digits following
  297. <code class="literal">V</code>. <code class="literal">V</code> with
  298. <code class="function">to_number</code> divides in a similar manner.
  299. <code class="function">to_char</code> and <code class="function">to_number</code>
  300. do not support the use of
  301. <code class="literal">V</code> combined with a decimal point
  302. (e.g., <code class="literal">99.9V99</code> is not allowed).
  303. </p></li><li class="listitem"><p>
  304. <code class="literal">EEEE</code> (scientific notation) cannot be used in
  305. combination with any of the other formatting patterns or
  306. modifiers other than digit and decimal point patterns, and must be at the end of the format string
  307. (e.g., <code class="literal">9.99EEEE</code> is a valid pattern).
  308. </p></li></ul></div><p>
  309. </p><p>
  310. Certain modifiers can be applied to any template pattern to alter its
  311. behavior. For example, <code class="literal">FM99.99</code>
  312. is the <code class="literal">99.99</code> pattern with the
  313. <code class="literal">FM</code> modifier.
  314. <a class="xref" href="functions-formatting.html#FUNCTIONS-FORMATTING-NUMERICMOD-TABLE" title="Table 9.28. Template Pattern Modifiers for Numeric Formatting">Table 9.28</a> shows the
  315. modifier patterns for numeric formatting.
  316. </p><div class="table" id="FUNCTIONS-FORMATTING-NUMERICMOD-TABLE"><p class="title"><strong>Table 9.28. Template Pattern Modifiers for Numeric Formatting</strong></p><div class="table-contents"><table class="table" summary="Template Pattern Modifiers for Numeric Formatting" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Modifier</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td><code class="literal">FM</code> prefix</td><td>fill mode (suppress trailing zeroes and padding blanks)</td><td><code class="literal">FM99.99</code></td></tr><tr><td><code class="literal">TH</code> suffix</td><td>upper case ordinal number suffix</td><td><code class="literal">999TH</code></td></tr><tr><td><code class="literal">th</code> suffix</td><td>lower case ordinal number suffix</td><td><code class="literal">999th</code></td></tr></tbody></table></div></div><br class="table-break" /><p>
  317. <a class="xref" href="functions-formatting.html#FUNCTIONS-FORMATTING-EXAMPLES-TABLE" title="Table 9.29. to_char Examples">Table 9.29</a> shows some
  318. examples of the use of the <code class="function">to_char</code> function.
  319. </p><div class="table" id="FUNCTIONS-FORMATTING-EXAMPLES-TABLE"><p class="title"><strong>Table 9.29. <code class="function">to_char</code> Examples</strong></p><div class="table-contents"><table class="table" summary="to_char Examples" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Expression</th><th>Result</th></tr></thead><tbody><tr><td><code class="literal">to_char(current_timestamp, 'Day, DD  HH12:MI:SS')</code></td><td><code class="literal">'Tuesday  , 06  05:39:18'</code></td></tr><tr><td><code class="literal">to_char(current_timestamp, 'FMDay, FMDD  HH12:MI:SS')</code></td><td><code class="literal">'Tuesday, 6  05:39:18'</code></td></tr><tr><td><code class="literal">to_char(-0.1, '99.99')</code></td><td><code class="literal">'  -.10'</code></td></tr><tr><td><code class="literal">to_char(-0.1, 'FM9.99')</code></td><td><code class="literal">'-.1'</code></td></tr><tr><td><code class="literal">to_char(-0.1, 'FM90.99')</code></td><td><code class="literal">'-0.1'</code></td></tr><tr><td><code class="literal">to_char(0.1, '0.9')</code></td><td><code class="literal">' 0.1'</code></td></tr><tr><td><code class="literal">to_char(12, '9990999.9')</code></td><td><code class="literal">'    0012.0'</code></td></tr><tr><td><code class="literal">to_char(12, 'FM9990999.9')</code></td><td><code class="literal">'0012.'</code></td></tr><tr><td><code class="literal">to_char(485, '999')</code></td><td><code class="literal">' 485'</code></td></tr><tr><td><code class="literal">to_char(-485, '999')</code></td><td><code class="literal">'-485'</code></td></tr><tr><td><code class="literal">to_char(485, '9 9 9')</code></td><td><code class="literal">' 4 8 5'</code></td></tr><tr><td><code class="literal">to_char(1485, '9,999')</code></td><td><code class="literal">' 1,485'</code></td></tr><tr><td><code class="literal">to_char(1485, '9G999')</code></td><td><code class="literal">' 1 485'</code></td></tr><tr><td><code class="literal">to_char(148.5, '999.999')</code></td><td><code class="literal">' 148.500'</code></td></tr><tr><td><code class="literal">to_char(148.5, 'FM999.999')</code></td><td><code class="literal">'148.5'</code></td></tr><tr><td><code class="literal">to_char(148.5, 'FM999.990')</code></td><td><code class="literal">'148.500'</code></td></tr><tr><td><code class="literal">to_char(148.5, '999D999')</code></td><td><code class="literal">' 148,500'</code></td></tr><tr><td><code class="literal">to_char(3148.5, '9G999D999')</code></td><td><code class="literal">' 3 148,500'</code></td></tr><tr><td><code class="literal">to_char(-485, '999S')</code></td><td><code class="literal">'485-'</code></td></tr><tr><td><code class="literal">to_char(-485, '999MI')</code></td><td><code class="literal">'485-'</code></td></tr><tr><td><code class="literal">to_char(485, '999MI')</code></td><td><code class="literal">'485 '</code></td></tr><tr><td><code class="literal">to_char(485, 'FM999MI')</code></td><td><code class="literal">'485'</code></td></tr><tr><td><code class="literal">to_char(485, 'PL999')</code></td><td><code class="literal">'+485'</code></td></tr><tr><td><code class="literal">to_char(485, 'SG999')</code></td><td><code class="literal">'+485'</code></td></tr><tr><td><code class="literal">to_char(-485, 'SG999')</code></td><td><code class="literal">'-485'</code></td></tr><tr><td><code class="literal">to_char(-485, '9SG99')</code></td><td><code class="literal">'4-85'</code></td></tr><tr><td><code class="literal">to_char(-485, '999PR')</code></td><td><code class="literal">'&lt;485&gt;'</code></td></tr><tr><td><code class="literal">to_char(485, 'L999')</code></td><td><code class="literal">'DM 485'</code></td></tr><tr><td><code class="literal">to_char(485, 'RN')</code></td><td><code class="literal">'        CDLXXXV'</code></td></tr><tr><td><code class="literal">to_char(485, 'FMRN')</code></td><td><code class="literal">'CDLXXXV'</code></td></tr><tr><td><code class="literal">to_char(5.2, 'FMRN')</code></td><td><code class="literal">'V'</code></td></tr><tr><td><code class="literal">to_char(482, '999th')</code></td><td><code class="literal">' 482nd'</code></td></tr><tr><td><code class="literal">to_char(485, '"Good number:"999')</code></td><td><code class="literal">'Good number: 485'</code></td></tr><tr><td><code class="literal">to_char(485.8, '"Pre:"999" Post:" .999')</code></td><td><code class="literal">'Pre: 485 Post: .800'</code></td></tr><tr><td><code class="literal">to_char(12, '99V999')</code></td><td><code class="literal">' 12000'</code></td></tr><tr><td><code class="literal">to_char(12.4, '99V999')</code></td><td><code class="literal">' 12400'</code></td></tr><tr><td><code class="literal">to_char(12.45, '99V9')</code></td><td><code class="literal">' 125'</code></td></tr><tr><td><code class="literal">to_char(0.0004859, '9.99EEEE')</code></td><td><code class="literal">' 4.86e-04'</code></td></tr></tbody></table></div></div><br class="table-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-matching.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-datetime.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.7. Pattern Matching </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.9. Date/Time Functions and Operators</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1