func.sgml 36.6 KB
Newer Older
T
Thomas G. Lockhart 已提交
1 2
 <chapter id="functions">
  <title id="functions-title">Functions</title>
3

T
Thomas G. Lockhart 已提交
4 5 6 7 8 9
  <abstract>
   <para>
    Describes the built-in functions available
    in <productname>Postgres</productname>.
   </para>
  </abstract>
10

T
Thomas G. Lockhart 已提交
11
  <para>
12 13 14
   Many data types have functions available for conversion to other related types.
   In addition, there are some type-specific functions. Some functions are also
   available through operators and may be documented as operators only.
T
Thomas G. Lockhart 已提交
15
  </para>
16

17 18
  <sect1>
   <title id="sql-funcs">SQL Functions</title>
19

20 21 22 23 24 25 26 27
   <para>
    <quote><acronym>SQL</acronym> functions</quote> are constructs
    defined by the <acronym>SQL92</acronym> standard which have
    function-like syntax but which can not be implemented as simple
    functions. 
   </para>

   <para>
T
Thomas G. Lockhart 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    <table tocentry="1">
     <title>SQL Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> COALESCE(<replaceable class="parameter">list</replaceable>) </entry>
	<entry> non-NULL </entry>
	<entry> return first non-NULL value in list </entry>
	<entry> COALESCE(<replaceable class="parameter">r"</replaceable>le>, <replaceable
	  class="parameter">c2</replaceable> + 5, 0) </entry>
       </row>
       <row>
48 49 50 51
	<entry> NULLIF(<replaceable class="parameter">input</replaceable>,<replaceable class="parameter">value</replaceable>) </entry>
	<entry> <replaceable class="parameter">input</replaceable> or NULL </entry>
	<entry> return NULL if <replaceable class="parameter">input</replaceable> = <replaceable class="parameter">value</replaceable> </entry>
	<entry> NULLIF(<replaceable class="parameter">c1</replaceable>, 'N/A')</entry>
T
Thomas G. Lockhart 已提交
52 53 54 55 56 57 58
       </row>
       <row>
	<entry> CASE WHEN <replaceable class="parameter">expr</replaceable> THEN <replaceable class="parameter">expr</replaceable> [...] ELSE <replaceable class="parameter">expr</replaceable> END </entry>
	<entry> <replaceable class="parameter">expr</replaceable> </entry>
	<entry> return expression for first true clause </entry>
	<entry> CASE WHEN <replaceable class="parameter">c1</replaceable> = 1 THEN 'match' ELSE 'no match' END </entry>
       </row>
59 60 61 62 63 64 65 66 67
      </tbody>
     </tgroup>
    </table>
   </para>
  </sect1>

  <sect1>
   <title id="math-funcs">Mathematical Functions</title>

T
Thomas G. Lockhart 已提交
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
   <para>
    <table tocentry="1">
     <title>Mathematical Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> dexp(float8) </entry>
	<entry> float8 </entry>
	<entry> raise e to the specified exponent </entry>
	<entry> dexp(2.0) </entry>
       </row>
       <row>
	<entry> dpow(float8,float8) </entry>
	<entry> float8 </entry>
	<entry> raise a number to the specified exponent </entry>
	<entry> dpow(2.0, 16.0) </entry>
       </row>
       <row>
	<entry> float(int) </entry>
	<entry> float8 </entry>
	<entry> convert integer to floating point </entry>
	<entry> float(2) </entry>
       </row>
       <row>
	<entry> float4(int) </entry>
	<entry> float4 </entry>
	<entry> convert integer to floating point </entry>
	<entry> float4(2) </entry>
       </row>
       <row>
	<entry> integer(float) </entry>
	<entry> int </entry>
	<entry> convert floating point to integer </entry>
	<entry> integer(2.0) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
115
  </sect1>
116

117 118
  <sect1>
   <title>String Functions</title>
119

T
Thomas G. Lockhart 已提交
120
   <para>
121
    SQL92 defines string functions with specific syntax. Some of these
T
Thomas G. Lockhart 已提交
122
    are implemented using other <productname>Postgres</productname> functions.
123 124
    The supported string types for <acronym>SQL92</acronym> are
    <type>char</type>, <type>varchar</type>, and <type>text</type>.
T
Thomas G. Lockhart 已提交
125
   </para>
126

T
Thomas G. Lockhart 已提交
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
   <para>
    <table tocentry="1">
     <title><acronym>SQL92</acronym> String Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> char_length(string) </entry>
	<entry> int4 </entry>
	<entry> length of string </entry>
	<entry> char_length('jose') </entry>
       </row>
       <row>
	<entry> character_length(string) </entry>
	<entry> int4 </entry>
	<entry> length of string </entry>
	<entry> char_length('jose') </entry>
       </row>
       <row>
	<entry> lower(string) </entry>
	<entry> string </entry>
	<entry> convert string to lower case </entry>
	<entry> lower('TOM') </entry>
       </row>
       <row>
	<entry> octet_length(string) </entry>
	<entry> int4 </entry>
	<entry> storage length of string </entry>
	<entry> octet_length('jose') </entry>
       </row>
       <row>
	<entry> position(string in string) </entry>
	<entry> int4 </entry>
	<entry> location of specified substring </entry>
	<entry> position('o' in 'Tom') </entry>
       </row>
       <row>
	<entry> substring(string [from int] [for int]) </entry>
	<entry> string </entry>
	<entry> extract specified substring </entry>
	<entry> substring('Tom' from 2 for 2) </entry>
       </row>
       <row>
	<entry> trim([leading|trailing|both] [string] from string) </entry>
	<entry> string </entry>
	<entry> trim characters from string </entry>
	<entry> trim(both 'x' from 'xTomx') </entry>
       </row>
       <row>
	<entry> upper(text) </entry>
	<entry> text </entry>
	<entry> convert text to upper case </entry>
	<entry> upper('tom') </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
192

T
Thomas G. Lockhart 已提交
193
   <para>
194 195
    Many additional string functions are available for text, varchar(), and char() types.
    Some are used internally to implement the SQL92 string functions listed above.
T
Thomas G. Lockhart 已提交
196
   </para>
197

T
Thomas G. Lockhart 已提交
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
   <para>
    <table tocentry="1">
     <title>String Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> char(text) </entry>
	<entry> char </entry>
	<entry> convert text to char type </entry>
	<entry> char('text string') </entry>
       </row>
       <row>
	<entry> char(varchar) </entry>
	<entry> char </entry>
	<entry> convert varchar to char type </entry>
	<entry> char(varchar 'varchar string') </entry>
       </row>
       <row>
	<entry> initcap(text) </entry>
	<entry> text </entry>
	<entry> first letter of each word to upper case </entry>
	<entry> initcap('thomas') </entry>
       </row>
       <row>
	<entry> lpad(text,int,text) </entry>
	<entry> text </entry>
	<entry> left pad string to specified length </entry>
	<entry> lpad('hi',4,'??') </entry>
       </row>
       <row>
	<entry> ltrim(text,text) </entry>
	<entry> text </entry>
	<entry> left trim characters from text </entry>
	<entry> ltrim('xxxxtrim','x') </entry>
       </row>
       <row>
	<entry> textpos(text,text) </entry>
	<entry> text </entry>
	<entry> locate specified substring </entry>
	<entry> position('high','ig') </entry>
       </row>
       <row>
	<entry> rpad(text,int,text) </entry>
	<entry> text </entry>
	<entry> right pad string to specified length </entry>
	<entry> rpad('hi',4,'x') </entry>
       </row>
       <row>
	<entry> rtrim(text,text) </entry>
	<entry> text </entry>
	<entry> right trim characters from text </entry>
	<entry> rtrim('trimxxxx','x') </entry>
       </row>
       <row>
	<entry> substr(text,int[,int]) </entry>
	<entry> text </entry>
	<entry> extract specified substring </entry>
	<entry> substr('hi there',3,5) </entry>
       </row>
       <row>
	<entry> text(char) </entry>
	<entry> text </entry>
	<entry> convert char to text type </entry>
	<entry> text('char string') </entry>
       </row>
       <row>
	<entry> text(varchar) </entry>
	<entry> text </entry>
	<entry> convert varchar to text type </entry>
	<entry> text(varchar 'varchar string') </entry>
       </row>
       <row>
	<entry> translate(text,from,to) </entry>
	<entry> text </entry>
	<entry> convert character in string </entry>
	<entry> translate('12345', '1', 'a') </entry>
       </row>
       <row>
	<entry> varchar(char) </entry>
	<entry> varchar </entry>
	<entry> convert char to varchar type </entry>
	<entry> varchar('char string') </entry>
       </row>
       <row>
	<entry> varchar(text) </entry>
	<entry> varchar </entry>
	<entry> convert text to varchar type </entry>
	<entry> varchar('text string') </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
299

300 301 302 303
   <para>
    Most functions explicitly defined for text will work for char() and varchar() arguments.
   </para>
  </sect1>
304

305 306
  <sect1>
   <title>Date/Time Functions</title>
307

308 309 310 311
   <para>
    The date/time functions provide a powerful set of tools
    for manipulating various date/time types.
   </para>
312

T
Thomas G. Lockhart 已提交
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
   <para>
    <table tocentry="1">
     <title>Date/Time Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> abstime(datetime) </entry>
	<entry> abstime </entry>
	<entry> convert to abstime </entry>
	<entry> abstime('now'::datetime) </entry>
       </row>
       <row>
	<entry> age(datetime,datetime) </entry>
	<entry> timespan </entry>
	<entry> preserve months and years </entry>
	<entry> age('now','1957-06-13'::datetime) </entry>
       </row>
       <row>
	<entry> datetime(abstime) </entry>
	<entry> datetime </entry>
	<entry> convert to datetime </entry>
	<entry> datetime('now'::abstime) </entry>
       </row>
       <row>
	<entry> datetime(date) </entry>
	<entry> datetime </entry>
	<entry> convert to datetime </entry>
	<entry> datetime('today'::date) </entry>
       </row>
       <row>
	<entry> datetime(date,time) </entry>
	<entry> datetime </entry>
	<entry> convert to datetime </entry>
	<entry> datetime('1998-02-24'::datetime, '23:07'::time); </entry>
  </row>
       <row>
	<entry> date_part(text,datetime) </entry>
	<entry> float8 </entry>
	<entry> portion of date </entry>
	<entry> date_part('dow','now'::datetime) </entry>
       </row>
       <row>
	<entry> date_part(text,timespan) </entry>
	<entry> float8 </entry>
	<entry> portion of time </entry>
	<entry> date_part('hour','4 hrs 3 mins'::timespan) </entry>
       </row>
       <row>
	<entry> date_trunc(text,datetime) </entry>
	<entry> datetime </entry>
	<entry> truncate date </entry>
	<entry> date_trunc('month','now'::abstime) </entry>
       </row>
       <row>
	<entry> isfinite(abstime) </entry>
	<entry> bool </entry>
	<entry> a finite time? </entry>
	<entry> isfinite('now'::abstime) </entry>
       </row>
       <row>
	<entry> isfinite(datetime) </entry>
	<entry> bool </entry>
	<entry> a finite time? </entry>
	<entry> isfinite('now'::datetime) </entry>
       </row>
       <row>
	<entry> isfinite(timespan) </entry>
	<entry> bool </entry>
	<entry> a finite time? </entry>
	<entry> isfinite('4 hrs'::timespan) </entry>
       </row>
       <row>
	<entry> reltime(timespan) </entry>
	<entry> reltime </entry>
	<entry> convert to reltime </entry>
	<entry> reltime('4 hrs'::timespan) </entry>
       </row>
       <row>
	<entry> timespan(reltime) </entry>
	<entry> timespan </entry>
	<entry> convert to timespan </entry>
	<entry> timespan('4 hours'::reltime) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
408

T
Thomas G. Lockhart 已提交
409
   <para>
410
    For the
T
Thomas G. Lockhart 已提交
411
    <function>date_part</function> and <function>date_trunc</function>
412 413 414 415
    functions, arguments can be
    `year', `month', `day', `hour', `minute', and `second',
    as well as the more specialized quantities
    `decade', `century', `millenium', `millisecond', and `microsecond'.
T
Thomas G. Lockhart 已提交
416 417 418 419 420
    <function>date_part</function> allows `dow'
    to return day of week and `epoch' to return seconds since 1970
    (for <type>datetime</type>)
    or 'epoch' to return total elapsed seconds (for <type>timespan</type>).
   </para>
421
  </sect1>
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
  
  <sect1>

   <title id="formatting-funcs"> Formatting Functions </title>

   <note>
    <title>Author</title>
    <para>
     Written by 
     <ulink url="mailto:zakkr@zf.jcu.cz">Karel Zak</ulink>
     on 2000-01-24.
    </para>
   </note>

   <para>
    Formatting functions provide a powerful set of tools for converting 
    various datetypes (date/time, int, float, numeric) to formatted strings 
    and reverse convert from formatted strings to original datetypes. 
   </para>

   <para>
    <table tocentry="1">
     <title>Formatting Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> to_char(datetime, text) </entry>
	<entry> text </entry>
	<entry> convert datetime to string </entry>
	<entry> to_char('now'::datetime, 'HH12:MI:SS') </entry>
       </row>
       <row>
	<entry> to_char(timestamp, text) </entry>
	<entry> text </entry>
	<entry> convert timestamp to string </entry>
	<entry> to_char( now(), 'HH12:MI:SS') </entry>
       </row>
       <row>
	<entry> to_char(int, text) </entry>
	<entry> text </entry>
	<entry> convert int4/int8 to string </entry>
	<entry> to_char(125, '999') </entry>
       </row>
       <row>
	<entry> to_char(float, text) </entry>
	<entry> text </entry>
	<entry> convert float4/float8 to string </entry>
	<entry> to_char(125.8, '999D9') </entry>
       </row>
       <row>
	<entry> to_char(numeric, text) </entry>
	<entry> text </entry>
	<entry> convert numeric to string </entry>
	<entry> to_char(-125.8, '999D99S') </entry>
       </row>       
       <row>
	<entry> to_datetime(text, text) </entry>
	<entry> datetime </entry>
	<entry> convert string to datetime </entry>
	<entry> to_datetime('05 Dec 2000 13', 'DD Mon YYYY HH') </entry>
       </row>
       <row>
	<entry> to_date(text, text) </entry>
	<entry> date </entry>
	<entry> convert string to date </entry>
	<entry> to_date('05 Dec 2000', 'DD Mon YYYY') </entry>
       </row>
       <row>
	<entry> to_timestamp(text, text) </entry>
	<entry> date </entry>
	<entry> convert string to timestamp </entry>
	<entry> to_timestamp('05 Dec 2000', 'DD Mon YYYY') </entry>
       </row>
       <row>
	<entry> to_number(text, text) </entry>
	<entry> numeric </entry>
	<entry> convert string to numeric </entry>
	<entry> to_number('12,454.8-', '99G999D9S') </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
    For all formatting functions is second argument format-picture. 
   </para>

   <para>
    <table tocentry="1">
521
     <title>Format-pictures for date/time to_char() version.</title>
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
     <tgroup cols="2">
      <thead>
       <row>
	<entry>Format-picture</entry>
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> HH </entry>
	<entry> hour of day (01-12) </entry>
       </row>
       <row>
	<entry> HH12 </entry>
	<entry> hour of day (01-12) </entry>
       </row>       
       <row>
	<entry> MI </entry>
	<entry> minute (00-59) </entry>
       </row>   
       <row>
	<entry> SS </entry>
	<entry> socond (00-59) </entry>
       </row>
       <row>
	<entry> SSSS </entry>
	<entry> seconds past midnight (0-86399) </entry>
       </row>
       <row>
	<entry> Y,YYY </entry>
	<entry> year (4 and more digits) with comma </entry>
       </row>
       <row>
	<entry> YYYY </entry>
	<entry> year (4 and more digits) </entry>
       </row>
       <row>
	<entry> YYY </entry>
	<entry> last 3 digits of year </entry>
       </row>
       <row>
	<entry> YY </entry>
	<entry> last 2 digits of year </entry>
       </row>
       <row>
	<entry> Y </entry>
	<entry> last digit of year </entry>
       </row>
       <row>
	<entry> MONTH </entry>
	<entry> full month name (9-letters) - all characters is upper  </entry>
       </row>
       <row>
	<entry> Month </entry>
	<entry> full month name (9-letters) - first character is upper  </entry>
       </row>
       <row>
	<entry> month </entry>
	<entry> full month name (9-letters) - all characters is lower </entry>
       </row>
       <row>
	<entry> MON </entry>
	<entry> abbreviated month name (3-letters) - all characters is upper </entry>
       </row>
       <row>
	<entry> Mon </entry>
	<entry> abbreviated month name (3-letters) - first character is upper </entry>
       </row>
       <row>
	<entry> mon </entry>
	<entry> abbreviated month name (3-letters) - all characters is lower </entry>
       </row>
       <row>
	<entry> MM </entry>
	<entry> month (01-12) </entry>
       </row>
       <row>
	<entry> DAY </entry>
	<entry> full day name (9-letters) - all characters is upper </entry>
       </row>
       <row>
	<entry> Day </entry>
	<entry> full day name (9-letters) - first character is upper </entry>
       </row>
       <row>
	<entry> day </entry>
	<entry> full day name (9-letters) - all characters is lower </entry>
       </row>
       <row>
	<entry> DY </entry>
	<entry> abbreviated day name (3-letters) - all characters is upper </entry>
       </row>
       <row>
	<entry> Dy </entry>
	<entry> abbreviated day name (3-letters) - first character is upper </entry>
       </row>
       <row>
	<entry> dy </entry>
	<entry> abbreviated day name (3-letters) - all characters is upper </entry>
       </row>
       <row>
	<entry> DDD </entry>
	<entry> day of year (001-366) </entry>
       </row>
       <row>
	<entry> DD </entry>
	<entry> day of month (01-31) </entry>
       </row>
       <row>
	<entry> D </entry>
	<entry> day of week (1-7; SUN=1) </entry>
       </row>
       <row>
	<entry> W </entry>
	<entry> week of month </entry>
       </row> 
       <row>
	<entry> WW </entry>
	<entry> week number of year </entry>
       </row>
       <row>
	<entry> CC </entry>
	<entry> century (2-digits) </entry>
       </row>
       <row>
	<entry> J </entry>
	<entry> julian day (days since January 1, 4712 BC) </entry>
       </row>
       <row>
	<entry> Q </entry>
	<entry> quarter </entry>
       </row>
       <row>
	<entry> RM </entry>
	<entry> month in roman numeral (I-XII; I=JAN) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
    All format-pictures	allow use suffixes (postfix / prefix). The suffix is 
665
    always valid for a near format-picture. The 'FX' is global prefix only.      
666 667 668 669
   </para>

   <para>
    <table tocentry="1">
670
     <title>Suffixes for format-pictures for date/time to_char() version.</title>
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
     <tgroup cols="3">
      <thead>
       <row>
	<entry>Suffix</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> FM </entry>
	<entry> fill mode - prefix </entry>
	<entry> FMMonth </entry>
       </row>
       <row>
	<entry> TH </entry>
	<entry> upper ordinal number - postfix </entry>
	<entry> DDTH </entry>
       </row>	
       <row>
	<entry> th </entry>
	<entry> lower ordinal number - postfix </entry>
	<entry> DDTH </entry>
       </row>
       <row>
	<entry> FX </entry>
	<entry> FX - (Fixed format) global format-picture switch. 
698
	 The TO_DATETIME / TO_DATE skip blank space if this option is
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	 not use. Must by used as first item in formt-picture. </entry>
	<entry> FX Month DD Day </entry>
       </row>	
       <row>
	<entry> SP </entry>
	<entry> spell mode (not implement now)</entry>
	<entry> DDSP </entry>
       </row>       
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
     '\' - must be use as double \\, example '\\HH\\MI\\SS' 
   </para>
   <para>
     '"' - string between a quotation marks is skipen and not is parsed. 
717
     If you want write '"' to output you must use \\", example '\\"YYYY Month\\"'.
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 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
   </para>
   <para>
     text - the PostgreSQL's to_char() support text without '"', but string 
     between a quotation marks is fastly and you have guarantee, that a text 
     not will interpreted as a keyword (format-picture), exapmle '"Hello Year: "YYYY'.  
   </para>
   
   <para>
    <table tocentry="1">
     <title>Format-pictures for number (int/float/numeric) to_char() version.</title>
     <tgroup cols="2">
      <thead>
       <row>
	<entry>Format-picture</entry>
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> 9 </entry>
	<entry> return value with the specified number of digits, and if digit is
	 not available use blank space </entry>
       </row>
       <row>
	<entry> 0 </entry>
	<entry> as 9, but instead blank space use zero </entry>
       </row>
       <row>
	<entry> . (period) </entry>
	<entry> decimal point </entry>
       </row>       
       <row>
	<entry> , (comma) </entry>
	<entry> group (thousand) separator </entry>
       </row>
       <row>
	<entry> PR </entry>
	<entry> return negative value in angle brackets </entry>
       </row>
       <row>
	<entry> S </entry>
	<entry> return negatice value with minus sign (use locales) </entry>
       </row>
       <row>
	<entry> L </entry>
	<entry> currency symbol (use locales) </entry>
       </row>
       <row>
	<entry> D </entry>
	<entry> decimal point (use locales) </entry>
       </row>
       <row>
	<entry> G </entry>
	<entry> group separator (use locales) </entry>
       </row>
       <row>
	<entry> MI </entry>
	<entry> return minus sign on specified position (if number < 0) </entry>
       </row>
       <row>
	<entry> PL </entry>
779 780 781 782 783
	<entry> return plus sign on specified position (if number > 0) - PostgreSQL extension </entry>
       </row>
       <row>
	<entry> SG </entry>
	<entry> return plus/minus sign on specified position - PostgreSQL extension </entry>
784 785 786 787 788 789 790
       </row>
       <row>
	<entry> RN </entry>
	<entry> return number as roman number (number must be between 1 and 3999) </entry>
       </row>
       <row>
	<entry> TH or th </entry>
791
	<entry> convert number to ordinal number (not convert numbers under zero and decimal numbers) - PostgreSQL extension </entry>
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
       </row>
       <row>
	<entry> V </entry>
	<entry> arg1 * (10 ^ n); - return a value multiplied by 10^n (where 'n' is number of '9's after the 'V'). 
	        The to_char() not support use 'V' and decimal poin together, example "99.9V99".   </entry>
       </row>
       <row>
	<entry> EEEE </entry>
	<entry> science numbers. Now not supported. </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
808 809 810 811 812 813
      Note: A sign formatted via 'SG', 'PL' or 'MI' is not anchor in number;
      to_char(-12, 'S9999') produce: <ProgramListing> '  -12' </ProgramListing>, 
      but to_char(-12, 'MI9999') produce: <ProgramListing> '-  12' </ProgramListing>. 
      The Oracle not allow use 'MI' ahead of '9', in the Oracle must be it always
      after '9'.
   </para>   
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

   <para>
    <table tocentry="1">
     <title> The to_char() examples. </title>
     <tgroup cols="2">
      <thead>
       <row>
	<entry>Input</entry>
	<entry>Output</entry>
       </row>
      </thead>
      <tbody>
       <row>
        <entry> to_char(now(), 'Day, HH12:MI:SS') </entry>
        <entry><ProgramListing> 'Tuesday  , 05:39:18' </ProgramListing></entry>
       </row>   
       <row>
        <entry> to_char(now(), 'FMDay, HH12:MI:SS') </entry>
        <entry><ProgramListing> 'Tuesday, 05:39:18' </ProgramListing></entry>
       </row>          
       <row>
835 836 837 838 839 840
        <entry> to_char( -0.1, '99.99') </entry>
        <entry><ProgramListing> ' -.10' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( -0.1, 'FM9.99') </entry>
        <entry><ProgramListing> '-.1' </ProgramListing></entry>
841 842 843 844 845 846
       </row>
       <row>
        <entry> to_char( 0.1, '0.9') </entry>
        <entry><ProgramListing> ' 0.1' </ProgramListing></entry>
       </row>
       <row>
847 848 849 850 851 852
        <entry> to_char( 12, '9990999.9') </entry>
        <entry><ProgramListing> '    0012.0' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 12, 'FM9990999.9') </entry>
        <entry><ProgramListing> '0012' </ProgramListing></entry>
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
       </row>
       <row>
        <entry> to_char( 485, '999') </entry>
        <entry><ProgramListing> ' 485' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( -485, '999') </entry>
        <entry><ProgramListing> '-485' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 485, '9 9 9') </entry>
        <entry><ProgramListing> ' 4 8 5' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 1485,	'9,999') </entry>	
        <entry><ProgramListing> ' 1,485' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 1485,	'9G999') </entry>
        <entry><ProgramListing> ' 1 485' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 148.5, '999.999') </entry>
        <entry><ProgramListing> ' 148.500' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 148.5, '999D999') </entry>
        <entry><ProgramListing> ' 148,500' </ProgramListing></entry>	 
       </row>
       <row>
        <entry> to_char( 3148.5,'9G999D999') </entry>
        <entry><ProgramListing> ' 3 148,500' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( -485,	'999S') </entry>
        <entry><ProgramListing> '485-'	</ProgramListing></entry>
       </row>
       <row>		
        <entry> to_char( -485,	'999MI') </entry>
        <entry><ProgramListing> '485-'	</ProgramListing></entry>	
       </row>
       <row>
        <entry> to_char( 485,	'999MI') </entry>
        <entry><ProgramListing> '485' </ProgramListing></entry>		
       </row>
       <row>
        <entry> to_char( 485,	'PL999') </entry>
        <entry><ProgramListing> '+485'	</ProgramListing></entry>	
       </row>
       <row>		
        <entry> to_char( 485,	'SG999') </entry>
        <entry><ProgramListing> '+485'	</ProgramListing></entry>	
       </row>
       <row>
        <entry> to_char( -485,	'SG999') </entry>
        <entry><ProgramListing> '-485'	</ProgramListing></entry>	
       </row>
       <row>
        <entry> to_char( -485,	'9SG99') </entry>
        <entry><ProgramListing> '4-85'	</ProgramListing></entry>	
       </row>
       <row>
        <entry> to_char( -485,	'999PR') </entry>
        <entry><ProgramListing> '<485>' </ProgramListing></entry>		
       </row>
       <row>
        <entry> to_char( 485,	'L999')	</entry>
        <entry><ProgramListing> 'DM 485' </ProgramListing></entry>	 
       </row>
       <row>
        <entry> to_char( 485,	'RN') </entry>		
        <entry><ProgramListing> '        CDLXXXV' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 485,	'FMRN')	</entry>	
        <entry><ProgramListing> 'CDLXXXV' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 5.2,	'FMRN')	</entry>
        <entry><ProgramListing> 'V' </ProgramListing></entry>		
       </row>
       <row>
        <entry> to_char( 482,	'999th') </entry>
        <entry><ProgramListing> ' 482nd' </ProgramListing></entry>				
       </row>
       <row>
        <entry> to_char( 485,	'"Good number:"999') </entry>
        <entry><ProgramListing> 'Good number: 485' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 485.8, '"Pre-decimal:"999" Post-decimal:" .999') </entry>
        <entry><ProgramListing> 'Pre-decimal: 485 Post-decimal: .800' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 12, '99V999') </entry>		
        <entry><ProgramListing> ' 12000' </ProgramListing></entry>
       </row>
       <row>
        <entry> to_char( 12.4, '99V999') </entry>
        <entry><ProgramListing> ' 12400' </ProgramListing></entry>
       </row>
       <row>		
        <entry> to_char( 12.45, '99V9') </entry>
        <entry><ProgramListing> ' 125' </ProgramListing></entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

  </sect1>



967 968
  <sect1>
   <title>Geometric Functions</title>
969

970 971 972 973
   <para>
    The geometric types point, box, lseg, line, path, polygon, and
    circle have a large set of native support functions.
   </para>
974

T
Thomas G. Lockhart 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
   <para>
    <table tocentry="1">
     <title>Geometric Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> area(box) </entry>
	<entry> float8 </entry>
	<entry> area of box </entry>
	<entry> area('((0,0),(1,1))'::box) </entry>
       </row>
       <row>
	<entry> area(circle) </entry>
	<entry> float8 </entry>
	<entry> area of circle </entry>
	<entry> area('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> box(box,box) </entry>
	<entry> box </entry>
	<entry> boxes to intersection box </entry>
	<entry> box('((0,0),(1,1))','((0.5,0.5),(2,2))') </entry>
       </row>
       <row>
	<entry> center(box) </entry>
	<entry> point </entry>
	<entry> center of object </entry>
	<entry> center('((0,0),(1,2))'::box) </entry>
       </row>
       <row>
	<entry> center(circle) </entry>
	<entry> point </entry>
	<entry> center of object </entry>
	<entry> center('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> diameter(circle) </entry>
	<entry> float8 </entry>
	<entry> diameter of circle </entry>
	<entry> diameter('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> height(box) </entry>
	<entry> float8 </entry>
	<entry> vertical size of box </entry>
	<entry> height('((0,0),(1,1))'::box) </entry>
       </row>
       <row>
	<entry> isclosed(path) </entry>
	<entry> bool </entry>
	<entry> a closed path? </entry>
	<entry> isclosed('((0,0),(1,1),(2,0))'::path) </entry>
       </row>
       <row>
	<entry> isopen(path) </entry>
	<entry> bool </entry>
	<entry> an open path? </entry>
	<entry> isopen('[(0,0),(1,1),(2,0)]'::path) </entry>
       </row>
       <row>
	<entry> length(lseg) </entry>
	<entry> float8 </entry>
	<entry> length of line segment </entry>
	<entry> length('((-1,0),(1,0))'::lseg) </entry>
       </row>
       <row>
	<entry> length(path) </entry>
	<entry> float8 </entry>
	<entry> length of path </entry>
	<entry> length('((0,0),(1,1),(2,0))'::path) </entry>
       </row>
       <row>
	<entry> pclose(path) </entry>
	<entry> path </entry>
	<entry> convert path to closed </entry>
	<entry> popen('[(0,0),(1,1),(2,0)]'::path) </entry>
       </row>
       <row>
	<entry> point(lseg,lseg) </entry>
	<entry> point </entry>
	<entry> intersection </entry>
	<entry> point('((-1,0),(1,0))'::lseg,'((-2,-2),(2,2))'::lseg) </entry>
       </row>
       <row>
	<entry> points(path) </entry>
	<entry> int4 </entry>
	<entry> number of points </entry>
	<entry> points('[(0,0),(1,1),(2,0)]'::path) </entry>
       </row>
       <row>
	<entry> popen(path) </entry>
	<entry> path </entry>
	<entry> convert path to open </entry>
	<entry> popen('((0,0),(1,1),(2,0))'::path) </entry>
       </row>
       <row>
	<entry> radius(circle) </entry>
	<entry> float8 </entry>
	<entry> radius of circle </entry>
	<entry> radius('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> width(box) </entry>
	<entry> float8 </entry>
	<entry> horizontal size </entry>
	<entry> width('((0,0),(1,1))'::box) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
1094

T
Thomas G. Lockhart 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
   <para>
    <table tocentry="1">
     <title>Geometric Type Conversion Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> box(circle) </entry>
	<entry> box </entry>
	<entry> convert circle to box </entry>
	<entry> box('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> box(point,point) </entry>
	<entry> box </entry>
	<entry> convert points to box </entry>
	<entry> box('(0,0)'::point,'(1,1)'::point) </entry>
       </row>
       <row>
	<entry> box(polygon) </entry>
	<entry> box </entry>
	<entry> convert polygon to box </entry>
	<entry> box('((0,0),(1,1),(2,0))'::polygon) </entry>
       </row>
       <row>
	<entry> circle(box) </entry>
	<entry> circle </entry>
	<entry> convert to circle </entry>
	<entry> circle('((0,0),(1,1))'::box) </entry>
       </row>
       <row>
	<entry> circle(point,float8) </entry>
	<entry> circle </entry>
	<entry> convert to circle </entry>
	<entry> circle('(0,0)'::point,2.0) </entry>
       </row>
       <row>
	<entry> lseg(box) </entry>
	<entry> lseg </entry>
	<entry> convert diagonal to lseg </entry>
	<entry> lseg('((-1,0),(1,0))'::box) </entry>
       </row>
       <row>
	<entry> lseg(point,point) </entry>
	<entry> lseg </entry>
	<entry> convert to lseg </entry>
	<entry> lseg('(-1,0)'::point,'(1,0)'::point) </entry>
       </row>
       <row>
	<entry> path(polygon) </entry>
	<entry> point </entry>
	<entry> convert to path </entry>
	<entry> path('((0,0),(1,1),(2,0))'::polygon) </entry>
       </row>
       <row>
	<entry> point(circle) </entry>
	<entry> point </entry>
	<entry> convert to point (center) </entry>
	<entry> point('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> point(lseg,lseg) </entry>
	<entry> point </entry>
	<entry> convert to point (intersection) </entry>
	<entry> point('((-1,0),(1,0))'::lseg, '((-2,-2),(2,2))'::lseg) </entry>
       </row>
       <row>
	<entry> point(polygon) </entry>
	<entry> point </entry>
	<entry> center of polygon </entry>
	<entry> point('((0,0),(1,1),(2,0))'::polygon) </entry>
       </row>
       <row>
	<entry> polygon(box) </entry>
	<entry> polygon </entry>
	<entry> convert to polygon with 12 points </entry>
	<entry> polygon('((0,0),(1,1))'::box) </entry>
       </row>
       <row>
	<entry> polygon(circle) </entry>
	<entry> polygon </entry>
	<entry> convert to 12-point polygon </entry>
	<entry> polygon('((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> polygon(<replaceable class="parameter">npts</replaceable>,circle) </entry>
	<entry> polygon </entry>
	<entry> convert to <replaceable class="parameter">npts</replaceable> polygon </entry>
	<entry> polygon(12,'((0,0),2.0)'::circle) </entry>
       </row>
       <row>
	<entry> polygon(path) </entry>
	<entry> polygon </entry>
	<entry> convert to polygon </entry>
	<entry> polygon('((0,0),(1,1),(2,0))'::path) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
1202

T
Thomas G. Lockhart 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
   <para>
    <table tocentry="1">
     <title>Geometric Upgrade Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> isoldpath(path) </entry>
	<entry> path </entry>
	<entry> test path for pre-v6.1 form </entry>
	<entry> isoldpath('(1,3,0,0,1,1,2,0)'::path) </entry>
       </row>
       <row>
	<entry> revertpoly(polygon) </entry>
	<entry> polygon </entry>
	<entry> convert pre-v6.1 polygon </entry>
	<entry> revertpoly('((0,0),(1,1),(2,0))'::polygon) </entry>
       </row>
       <row>
	<entry> upgradepath(path) </entry>
	<entry> path </entry>
	<entry> convert pre-v6.1 path </entry>
	<entry> upgradepath('(1,3,0,0,1,1,2,0)'::path) </entry>
       </row>
       <row>
	<entry> upgradepoly(polygon) </entry>
	<entry> polygon </entry>
	<entry> convert pre-v6.1 polygon </entry>
	<entry> upgradepoly('(0,1,2,0,1,0)'::polygon) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
1244
  </sect1>
1245

1246 1247
  <sect1>
   <title id="cidr-funcs">IP V4 Functions</title>
1248

T
Thomas G. Lockhart 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
   <para>
    <table tocentry="1">
     <title><productname>Postgres</productname>IP V4 Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> broadcast(cidr) </entry>
	<entry> text </entry>
	<entry> construct broadcast address as text </entry>
	<entry> broadcast('192.168.1.5/24') </entry>
       </row>
       <row>
	<entry> broadcast(inet) </entry>
	<entry> text </entry>
	<entry> construct broadcast address as text </entry>
	<entry> broadcast('192.168.1.5/24') </entry>
       </row>
       <row>
	<entry> host(inet) </entry>
	<entry> text </entry>
	<entry> extract host address as text </entry>
	<entry> host('192.168.1.5/24') </entry>
       </row>
       <row>
	<entry> masklen(cidr) </entry>
	<entry> int4 </entry>
	<entry> calculate netmask length </entry>
	<entry> masklen('192.168.1.5/24') </entry>
       </row>
       <row>
	<entry> masklen(inet) </entry>
	<entry> int4 </entry>
	<entry> calculate netmask length </entry>
	<entry> masklen('192.168.1.5/24') </entry>
       </row>
       <row>
	<entry> netmask(inet) </entry>
	<entry> text </entry>
	<entry> construct netmask as text </entry>
	<entry> netmask('192.168.1.5/24') </entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
1302

T
Thomas G. Lockhart 已提交
1303
  </sect1>
1304

1305
 </chapter>
1306

1307 1308 1309
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
1310
sgml-omittag:nil
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->