1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>9.8.Data Type Formatting Functions</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="functions.html" title="Chapter9.Functions and Operators">
<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">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="functions-formatting"></a>9.8.Data Type Formatting Functions</h2></div></div></div>
<a name="id608236"></a><a name="id608243"></a><a name="id608249"></a><a name="id608255"></a><a name="id608262"></a><p> The <span class="productname">PostgreSQL</span> formatting functions
provide a powerful set of tools for converting various data types
(date/time, integer, floating point, numeric) to formatted strings
and for converting from formatted strings to specific data types.
<a href="functions-formatting.html#functions-formatting-table" title="Table9.20.Formatting Functions">Table9.20, “Formatting Functions”</a> lists them.
These functions all follow a common calling convention: the first
argument is the value to be formatted and the second argument is a
template that defines the output or input format.
</p>
<p> The <code class="function">to_timestamp</code> function can also take a single
<code class="type">double precision</code> argument to convert from Unix epoch to
<code class="type">timestamp with time zone</code>.
(<code class="type">Integer</code> Unix epochs are implicitly cast to
<code class="type">double precision</code>.)
</p>
<div class="table">
<a name="functions-formatting-table"></a><p class="title"><b>Table9.20.Formatting Functions</b></p>
<div class="table-contents"><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><code class="literal"><code class="function">to_char</code>(<code class="type">timestamp</code>, <code class="type">text</code>)</code></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>(<code class="type">interval</code>, <code class="type">text</code>)</code></td>
<td><code class="type">text</code></td>
<td>convert interval to string</td>
<td><code class="literal">to_char(interval '15h2m12s', 'HH24:MI:SS')</code></td>
</tr>
<tr>
<td><code class="literal"><code class="function">to_char</code>(<code class="type">int</code>, <code class="type">text</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>,
<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>(<code class="type">numeric</code>, <code class="type">text</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><code class="literal"><code class="function">to_date</code>(<code class="type">text</code>, <code class="type">text</code>)</code></td>
<td><code class="type">date</code></td>
<td>convert string to date</td>
<td><code class="literal">to_date('05Dec2000', 'DDMonYYYY')</code></td>
</tr>
<tr>
<td><code class="literal"><code class="function">to_timestamp</code>(<code class="type">text</code>, <code class="type">text</code>)</code></td>
<td><code class="type">timestamp with time zone</code></td>
<td>convert string to time stamp</td>
<td><code class="literal">to_timestamp('05Dec2000', 'DDMonYYYY')</code></td>
</tr>
<tr>
<td><code class="literal"><code class="function">to_timestamp</code>(<code class="type">double precision</code>)</code></td>
<td><code class="type">timestamp with time zone</code></td>
<td>convert UNIX epoch to time stamp</td>
<td><code class="literal">to_timestamp(200120400)</code></td>
</tr>
<tr>
<td><code class="literal"><code class="function">to_number</code>(<code class="type">text</code>, <code class="type">text</code>)</code></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>
</tbody>
</table></div>
</div>
<br class="table-break"><p> In an output template string (for <code class="function">to_char</code>), there are certain patterns that are
recognized and replaced with appropriately-formatted data from the value
to be formatted. Any text that is not a template pattern is simply
copied verbatim. Similarly, in an input template string (for anything but <code class="function">to_char</code>), template patterns
identify the parts of the input data string to be looked at and the
values to be found there.
</p>
<p> <a href="functions-formatting.html#functions-formatting-datetime-table" title="Table9.21.Template Patterns for Date/Time Formatting">Table9.21, “Template Patterns for Date/Time Formatting”</a> shows the
template patterns available for formatting date and time values.
</p>
<div class="table">
<a name="functions-formatting-datetime-table"></a><p class="title"><b>Table9.21.Template Patterns for Date/Time Formatting</b></p>
<div class="table-contents"><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> or <code class="literal">A.M.</code> or
<code class="literal">PM</code> or <code class="literal">P.M.</code>
</td>
<td>meridian indicator (uppercase)</td>
</tr>
<tr>
<td>
<code class="literal">am</code> or <code class="literal">a.m.</code> or
<code class="literal">pm</code> or <code class="literal">p.m.</code>
</td>
<td>meridian indicator (lowercase)</td>
</tr>
<tr>
<td><code class="literal">Y,YYY</code></td>
<td>year (4 and more digits) with comma</td>
</tr>
<tr>
<td><code class="literal">YYYY</code></td>
<td>year (4 and 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 year (4 and more digits)</td>
</tr>
<tr>
<td><code class="literal">IYY</code></td>
<td>last 3 digits of ISO year</td>
</tr>
<tr>
<td><code class="literal">IY</code></td>
<td>last 2 digits of ISO year</td>
</tr>
<tr>
<td><code class="literal">I</code></td>
<td>last digits of ISO year</td>
</tr>
<tr>
<td>
<code class="literal">BC</code> or <code class="literal">B.C.</code> or
<code class="literal">AD</code> or <code class="literal">A.D.</code>
</td>
<td>era indicator (uppercase)</td>
</tr>
<tr>
<td>
<code class="literal">bc</code> or <code class="literal">b.c.</code> or
<code class="literal">ad</code> or <code class="literal">a.d.</code>
</td>
<td>era indicator (lowercase)</td>
</tr>
<tr>
<td><code class="literal">MONTH</code></td>
<td>full uppercase month name (blank-padded to 9 chars)</td>
</tr>
<tr>
<td><code class="literal">Month</code></td>
<td>full mixed-case month name (blank-padded to 9 chars)</td>
</tr>
<tr>
<td><code class="literal">month</code></td>
<td>full lowercase month name (blank-padded to 9 chars)</td>
</tr>
<tr>
<td><code class="literal">MON</code></td>
<td>abbreviated uppercase month name (3 chars)</td>
</tr>
<tr>
<td><code class="literal">Mon</code></td>
<td>abbreviated mixed-case month name (3 chars)</td>
</tr>
<tr>
<td><code class="literal">mon</code></td>
<td>abbreviated lowercase month name (3 chars)</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 uppercase day name (blank-padded to 9 chars)</td>
</tr>
<tr>
<td><code class="literal">Day</code></td>
<td>full mixed-case day name (blank-padded to 9 chars)</td>
</tr>
<tr>
<td><code class="literal">day</code></td>
<td>full lowercase day name (blank-padded to 9 chars)</td>
</tr>
<tr>
<td><code class="literal">DY</code></td>
<td>abbreviated uppercase day name (3 chars)</td>
</tr>
<tr>
<td><code class="literal">Dy</code></td>
<td>abbreviated mixed-case day name (3 chars)</td>
</tr>
<tr>
<td><code class="literal">dy</code></td>
<td>abbreviated lowercase day name (3 chars)</td>
</tr>
<tr>
<td><code class="literal">DDD</code></td>
<td>day of year (001-366)</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 week (1-7; Sunday is 1)</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>ISO week number of year (The first Thursday of the new year is in week 1.)</td>
</tr>
<tr>
<td><code class="literal">CC</code></td>
<td>century (2 digits)</td>
</tr>
<tr>
<td><code class="literal">J</code></td>
<td>Julian Day (days since January 1, 4712 BC)</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 Roman numerals (I-XII; I=January) (uppercase)</td>
</tr>
<tr>
<td><code class="literal">rm</code></td>
<td>month in Roman numerals (i-xii; i=January) (lowercase)</td>
</tr>
<tr>
<td><code class="literal">TZ</code></td>
<td>time-zone name (uppercase)</td>
</tr>
<tr>
<td><code class="literal">tz</code></td>
<td>time-zone name (lowercase)</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p> Certain modifiers may be applied to any template pattern to alter its
behavior. For example, <code class="literal">FMMonth</code>
is the <code class="literal">Month</code> pattern with the
<code class="literal">FM</code> modifier.
<a href="functions-formatting.html#functions-formatting-datetimemod-table" title="Table9.22.Template Pattern Modifiers for Date/Time Formatting">Table9.22, “Template Pattern Modifiers for Date/Time Formatting”</a> shows the
modifier patterns for date/time formatting.
</p>
<div class="table">
<a name="functions-formatting-datetimemod-table"></a><p class="title"><b>Table9.22.Template Pattern Modifiers for Date/Time Formatting</b></p>
<div class="table-contents"><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 padding blanks and zeroes)</td>
<td><code class="literal">FMMonth</code></td>
</tr>
<tr>
<td>
<code class="literal">TH</code> suffix</td>
<td>uppercase ordinal number suffix</td>
<td><code class="literal">DDTH</code></td>
</tr>
<tr>
<td>
<code class="literal">th</code> suffix</td>
<td>lowercase ordinal number suffix</td>
<td><code class="literal">DDth</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">FXMonthDDDay</code></td>
</tr>
<tr>
<td>
<code class="literal">SP</code> suffix</td>
<td>spell mode (not yet implemented)</td>
<td><code class="literal">DDSP</code></td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p> Usage notes for date/time formatting:
</p>
<div class="itemizedlist"><ul type="disc">
<li><p> <code class="literal">FM</code> suppresses leading zeroes and trailing blanks
that would otherwise be added to make the output of a pattern be
fixed-width.
</p></li>
<li><p> <code class="function">to_timestamp</code> and <code class="function">to_date</code>
skip multiple blank spaces in the input string if the <code class="literal">FX</code> option
is not used. <code class="literal">FX</code> must be specified as the first item
in the template. For example
<code class="literal">to_timestamp('2000JUN', 'YYYY MON')</code> is correct, but
<code class="literal">to_timestamp('2000JUN', 'FXYYYY MON')</code> returns an error,
because <code class="function">to_timestamp</code> expects one space only.
</p></li>
<li><p> Ordinary text is allowed in <code class="function">to_char</code>
templates and will be output literally. You can put a substring
in double quotes to force it to be interpreted as literal text
even if it contains pattern key words. For example, in
<code class="literal">'"Hello Year "YYYY'</code>, the <code class="literal">YYYY</code>
will be replaced by the year data, but the single <code class="literal">Y</code> in <code class="literal">Year</code>
will not be.
</p></li>
<li><p> If you want to have a double quote in the output you must
precede it with a backslash, for example <code class="literal">'\\"YYYY
Month\\"'</code>.
(Two backslashes are necessary because the backslash already
has a special meaning in a string constant.)
</p></li>
<li><p> The <code class="literal">YYYY</code> conversion from string to <code class="type">timestamp</code> or
<code class="type">date</code> has a restriction if you use a year with more than 4 digits. You must
use some non-digit character or template after <code class="literal">YYYY</code>,
otherwise the year is always interpreted as 4 digits. For example
(with the year 20000):
<code class="literal">to_date('200001131', 'YYYYMMDD')</code> will be
interpreted as a 4-digit year; instead use a non-digit
separator after the year, like
<code class="literal">to_date('20000-1131', 'YYYY-MMDD')</code> or
<code class="literal">to_date('20000Nov31', 'YYYYMonDD')</code>.
</p></li>
<li><p> In conversions from string to <code class="type">timestamp</code> or
<code class="type">date</code>, the <code class="literal">CC</code> field is ignored if there
is a <code class="literal">YYY</code>, <code class="literal">YYYY</code> or
<code class="literal">Y,YYY</code> field. If <code class="literal">CC</code> is used with
<code class="literal">YY</code> or <code class="literal">Y</code> then the year is computed
as <code class="literal">(CC-1)*100+YY</code>.
</p></li>
<li>
<p> Millisecond (<code class="literal">MS</code>) and microsecond (<code class="literal">US</code>)
values in a conversion from string to <code class="type">timestamp</code> are used as part of the
seconds after the decimal point. For example
<code class="literal">to_timestamp('12:3', 'SS:MS')</code> is not 3 milliseconds,
but 300, because the conversion counts it as 12 + 0.3 seconds.
This means for the format <code class="literal">SS:MS</code>, the input values
<code class="literal">12:3</code>, <code class="literal">12:30</code>, and <code class="literal">12:300</code> specify the
same number of milliseconds. To get three milliseconds, one must use
<code class="literal">12:003</code>, which the conversion counts as
12 + 0.003 = 12.003 seconds.
</p>
<p> Here is a more
complex example:
<code class="literal">to_timestamp('15:12:02.020.001230', 'HH:MI:SS.MS.US')</code>
is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
1230 microseconds = 2.021230 seconds.
</p>
</li>
<li><p><code class="function">to_char</code>'s day of the week numbering
(see the 'D' formatting pattern) is different from that of the
<code class="function">extract</code> function.
</p></li>
<li><p><code class="function">to_char(interval)</code> formats <code class="literal">HH</code> and
<code class="literal">HH12</code> as hours in a single day, while <code class="literal">HH24</code>
can output hours exceeding a single day, e.g. >24.
</p></li>
</ul></div>
<p>
</p>
<p> <a href="functions-formatting.html#functions-formatting-numeric-table" title="Table9.23.Template Patterns for Numeric Formatting">Table9.23, “Template Patterns for Numeric Formatting”</a> shows the
template patterns available for formatting numeric values.
</p>
<div class="table">
<a name="functions-formatting-numeric-table"></a><p class="title"><b>Table9.23.Template Patterns for Numeric Formatting</b></p>
<div class="table-contents"><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>value with the specified number of digits</td>
</tr>
<tr>
<td><code class="literal">0</code></td>
<td>value with leading zeros</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 (thousand) 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 < 0)</td>
</tr>
<tr>
<td><code class="literal">PL</code></td>
<td>plus sign in specified position (if number > 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>scientific notation (not implemented yet)</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p> Usage notes for numeric formatting:
</p>
<div class="itemizedlist"><ul type="disc">
<li><p> A sign formatted using <code class="literal">SG</code>, <code class="literal">PL</code>, or
<code class="literal">MI</code> is not anchored to
the number; for example,
<code class="literal">to_char(-12, 'S9999')</code> produces <code class="literal">'-12'</code>,
but <code class="literal">to_char(-12, 'MI9999')</code> produces <code class="literal">'-12'</code>.
The Oracle implementation does not allow the use of
<code class="literal">MI</code> ahead of <code class="literal">9</code>, but rather
requires that <code class="literal">9</code> precede
<code class="literal">MI</code>.
</p></li>
<li><p> <code class="literal">9</code> results in a value with the same number of
digits as there are <code class="literal">9</code>s. If a digit is
not available it outputs a space.
</p></li>
<li><p> <code class="literal">TH</code> does not convert values less than zero
and does not convert fractional numbers.
</p></li>
<li><p> <code class="literal">PL</code>, <code class="literal">SG</code>, and
<code class="literal">TH</code> are <span class="productname">PostgreSQL</span>
extensions.
</p></li>
<li><p> <code class="literal">V</code> effectively
multiplies the input values by
<code class="literal">10^<em class="replaceable"><code>n</code></em></code>, where
<em class="replaceable"><code>n</code></em> is the number of digits following
<code class="literal">V</code>.
<code class="function">to_char</code> does not support the use of
<code class="literal">V</code> combined with a decimal point.
(E.g., <code class="literal">99.9V99</code> is not allowed.)
</p></li>
</ul></div>
<p>
</p>
<p> <a href="functions-formatting.html#functions-formatting-examples-table" title="Table9.24.to_char Examples">Table9.24, “<code class="function">to_char</code> Examples”</a> shows some
examples of the use of the <code class="function">to_char</code> function.
</p>
<div class="table">
<a name="functions-formatting-examples-table"></a><p class="title"><b>Table9.24.<code class="function">to_char</code> Examples</b></p>
<div class="table-contents"><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,DDHH12:MI:SS')</code></td>
<td><code class="literal">'Tuesday,0605:39:18'</code></td>
</tr>
<tr>
<td><code class="literal">to_char(current_timestamp, 'FMDay,FMDDHH12:MI:SS')</code></td>
<td><code class="literal">'Tuesday,605: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, '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, '999')</code></td>
<td><code class="literal">'485'</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">'1485'</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">'3148,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">'<485>'</code></td>
</tr>
<tr>
<td><code class="literal">to_char(485, 'L999')</code></td>
<td><code class="literal">'DM485</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, '"Goodnumber:"999')</code></td>
<td><code class="literal">'Goodnumber:485'</code></td>
</tr>
<tr>
<td><code class="literal">to_char(485.8, '"Pre:"999"Post:".999')</code></td>
<td><code class="literal">'Pre:485Post:.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>
</tbody>
</table></div>
</div>
<br class="table-break">
</div></body>
</html>
|