@@ -5832,6 +5832,17 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
5832
5832
</para>
5833
5833
</note>
5834
5834
5835
+ <tip>
5836
+ <para>
5837
+ <function>to_timestamp</function> and <function>to_date</function>
5838
+ exist to handle input formats that cannot be converted by
5839
+ simple casting. For most standard date/time formats, simply casting the
5840
+ source string to the required data type works, and is much easier.
5841
+ Similarly, <function>to_number</> is unnecessary for standard numeric
5842
+ representations.
5843
+ </para>
5844
+ </tip>
5845
+
5835
5846
<para>
5836
5847
In a <function>to_char</> output template string, there are certain
5837
5848
patterns that are recognized and replaced with appropriately-formatted
@@ -6038,7 +6049,7 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6038
6049
</row>
6039
6050
<row>
6040
6051
<entry><literal>Q</literal></entry>
6041
- <entry>quarter (ignored by <function>to_date</> and <function>to_timestamp</>) </entry>
6052
+ <entry>quarter</entry>
6042
6053
</row>
6043
6054
<row>
6044
6055
<entry><literal>RM</literal></entry>
@@ -6156,20 +6167,6 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6156
6167
</para>
6157
6168
</listitem>
6158
6169
6159
- <listitem>
6160
- <para>
6161
- <function>to_timestamp</function> and <function>to_date</function>
6162
- exist to handle input formats that cannot be converted by
6163
- simple casting. These functions interpret input liberally,
6164
- with minimal error checking. While they produce valid output,
6165
- the conversion can yield unexpected results. For example,
6166
- input to these functions is not restricted by normal ranges,
6167
- thus <literal>to_date('20096040','YYYYMMDD')</literal> returns
6168
- <literal>2014-01-17</literal> rather than causing an error.
6169
- Casting does not have this behavior.
6170
- </para>
6171
- </listitem>
6172
-
6173
6170
<listitem>
6174
6171
<para>
6175
6172
Ordinary text is allowed in <function>to_char</function>
@@ -6195,7 +6192,8 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6195
6192
6196
6193
<listitem>
6197
6194
<para>
6198
- If the year format specification is less than four digits, e.g.
6195
+ In <function>to_timestamp</function> and <function>to_date</function>,
6196
+ if the year format specification is less than four digits, e.g.
6199
6197
<literal>YYY</>, and the supplied year is less than four digits,
6200
6198
the year will be adjusted to be nearest to the year 2020, e.g.
6201
6199
<literal>95</> becomes 1995.
@@ -6204,8 +6202,9 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6204
6202
6205
6203
<listitem>
6206
6204
<para>
6207
- The <literal>YYYY</literal> conversion from string to <type>timestamp</type> or
6208
- <type>date</type> has a restriction when processing years with more than 4 digits. You must
6205
+ In <function>to_timestamp</function> and <function>to_date</function>,
6206
+ the <literal>YYYY</literal> conversion has a restriction when
6207
+ processing years with more than 4 digits. You must
6209
6208
use some non-digit character or template after <literal>YYYY</literal>,
6210
6209
otherwise the year is always interpreted as 4 digits. For example
6211
6210
(with the year 20000):
@@ -6219,22 +6218,32 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6219
6218
6220
6219
<listitem>
6221
6220
<para>
6222
- In conversions from string to <type>timestamp</type> or
6223
- <type>date</type>, the <literal>CC</literal> (century) field is ignored
6221
+ In <function>to_timestamp</function> and <function>to_date</function>,
6222
+ the <literal>CC</literal> (century) field is accepted but ignored
6224
6223
if there is a <literal>YYY</literal>, <literal>YYYY</literal> or
6225
6224
<literal>Y,YYY</literal> field. If <literal>CC</literal> is used with
6226
- <literal>YY</literal> or <literal>Y</literal> then the year is computed
6227
- as the year in the specified century. If the century is
6225
+ <literal>YY</literal> or <literal>Y</literal> then the result is
6226
+ computed as that year in the specified century. If the century is
6228
6227
specified but the year is not, the first year of the century
6229
6228
is assumed.
6230
6229
</para>
6231
6230
</listitem>
6232
6231
6233
6232
<listitem>
6234
6233
<para>
6235
- An ISO 8601 week-numbering date (as distinct from a Gregorian date)
6236
- can be specified to <function>to_timestamp</function> and
6237
- <function>to_date</function> in one of two ways:
6234
+ In <function>to_timestamp</function> and <function>to_date</function>,
6235
+ weekday names or numbers (<literal>DAY</literal>, <literal>D</literal>,
6236
+ and related field types) are accepted but are ignored for purposes of
6237
+ computing the result. The same is true for quarter
6238
+ (<literal>Q</literal>) fields.
6239
+ </para>
6240
+ </listitem>
6241
+
6242
+ <listitem>
6243
+ <para>
6244
+ In <function>to_timestamp</function> and <function>to_date</function>,
6245
+ an ISO 8601 week-numbering date (as distinct from a Gregorian date)
6246
+ can be specified in one of two ways:
6238
6247
<itemizedlist>
6239
6248
<listitem>
6240
6249
<para>
@@ -6276,23 +6285,24 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6276
6285
6277
6286
<listitem>
6278
6287
<para>
6279
- In a conversion from string to <type>timestamp</type >, millisecond
6288
+ In <function>to_timestamp</function >, millisecond
6280
6289
(<literal>MS</literal>) or microsecond (<literal>US</literal>)
6281
- values are used as the
6290
+ fields are used as the
6282
6291
seconds digits after the decimal point. For example
6283
- <literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
6284
- but 300, because the conversion counts it as 12 + 0.3 seconds.
6285
- This means for the format <literal>SS:MS</literal>, the input values
6286
- <literal>12:3</literal>, <literal>12:30</literal>, and <literal>12:300</literal> specify the
6287
- same number of milliseconds. To get three milliseconds, one must use
6288
- <literal>12:003</literal>, which the conversion counts as
6292
+ <literal>to_timestamp('12.3', 'SS.MS')</literal> is not 3 milliseconds,
6293
+ but 300, because the conversion treats it as 12 + 0.3 seconds.
6294
+ So, for the format <literal>SS.MS</literal>, the input values
6295
+ <literal>12.3</literal>, <literal>12.30</literal>,
6296
+ and <literal>12.300</literal> specify the
6297
+ same number of milliseconds. To get three milliseconds, one must write
6298
+ <literal>12.003</literal>, which the conversion treats as
6289
6299
12 + 0.003 = 12.003 seconds.
6290
6300
</para>
6291
6301
6292
6302
<para>
6293
6303
Here is a more
6294
6304
complex example:
6295
- <literal>to_timestamp('15:12:02.020.001230', 'HH :MI:SS.MS.US')</literal>
6305
+ <literal>to_timestamp('15:12:02.020.001230', 'HH24 :MI:SS.MS.US')</literal>
6296
6306
is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
6297
6307
1230 microseconds = 2.021230 seconds.
6298
6308
</para>
@@ -6310,9 +6320,10 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
6310
6320
<listitem>
6311
6321
<para>
6312
6322
<function>to_char(interval)</function> formats <literal>HH</> and
6313
- <literal>HH12</> as shown on a 12-hour clock, i.e. zero hours
6314
- and 36 hours output as <literal>12</>, while <literal>HH24</>
6315
- outputs the full hour value, which can exceed 23 for intervals.
6323
+ <literal>HH12</> as shown on a 12-hour clock, for example zero hours
6324
+ and 36 hours both output as <literal>12</>, while <literal>HH24</>
6325
+ outputs the full hour value, which can exceed 23 in
6326
+ an <type>interval</> value.
6316
6327
</para>
6317
6328
</listitem>
6318
6329
0 commit comments