1-- 2-- STRINGS 3-- Test various data entry syntaxes. 4-- 5-- SQL string continuation syntax 6-- E021-03 character string literals 7SELECT 'first line' 8' - next line' 9 ' - third line' 10 AS "Three lines to one"; 11 Three lines to one 12------------------------------------- 13 first line - next line - third line 14(1 row) 15 16-- illegal string continuation syntax 17SELECT 'first line' 18' - next line' /* this comment is not allowed here */ 19' - third line' 20 AS "Illegal comment within continuation"; 21ERROR: syntax error at or near "' - third line'" 22LINE 3: ' - third line' 23 ^ 24-- Unicode escapes 25SET standard_conforming_strings TO on; 26SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061"; 27 data 28------ 29 data 30(1 row) 31 32SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*'; 33 dat\+000061 34------------- 35 dat\+000061 36(1 row) 37 38SELECT U&' \' UESCAPE '!' AS "tricky"; 39 tricky 40-------- 41 \ 42(1 row) 43 44SELECT 'tricky' AS U&"\" UESCAPE '!'; 45 \ 46-------- 47 tricky 48(1 row) 49 50SELECT U&'wrong: \061'; 51ERROR: invalid Unicode escape value at or near "\061'" 52LINE 1: SELECT U&'wrong: \061'; 53 ^ 54SELECT U&'wrong: \+0061'; 55ERROR: invalid Unicode escape value at or near "\+0061'" 56LINE 1: SELECT U&'wrong: \+0061'; 57 ^ 58SELECT U&'wrong: +0061' UESCAPE '+'; 59ERROR: invalid Unicode escape character at or near "+'" 60LINE 1: SELECT U&'wrong: +0061' UESCAPE '+'; 61 ^ 62SET standard_conforming_strings TO off; 63SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061"; 64ERROR: unsafe use of string constant with Unicode escapes 65LINE 1: SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061"; 66 ^ 67DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off. 68SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*'; 69ERROR: unsafe use of string constant with Unicode escapes 70LINE 1: SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061... 71 ^ 72DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off. 73SELECT U&' \' UESCAPE '!' AS "tricky"; 74ERROR: unsafe use of string constant with Unicode escapes 75LINE 1: SELECT U&' \' UESCAPE '!' AS "tricky"; 76 ^ 77DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off. 78SELECT 'tricky' AS U&"\" UESCAPE '!'; 79 \ 80-------- 81 tricky 82(1 row) 83 84SELECT U&'wrong: \061'; 85ERROR: unsafe use of string constant with Unicode escapes 86LINE 1: SELECT U&'wrong: \061'; 87 ^ 88DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off. 89SELECT U&'wrong: \+0061'; 90ERROR: unsafe use of string constant with Unicode escapes 91LINE 1: SELECT U&'wrong: \+0061'; 92 ^ 93DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off. 94SELECT U&'wrong: +0061' UESCAPE '+'; 95ERROR: unsafe use of string constant with Unicode escapes 96LINE 1: SELECT U&'wrong: +0061' UESCAPE '+'; 97 ^ 98DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off. 99RESET standard_conforming_strings; 100-- bytea 101SET bytea_output TO hex; 102SELECT E'\\xDeAdBeEf'::bytea; 103 bytea 104------------ 105 \xdeadbeef 106(1 row) 107 108SELECT E'\\x De Ad Be Ef '::bytea; 109 bytea 110------------ 111 \xdeadbeef 112(1 row) 113 114SELECT E'\\xDeAdBeE'::bytea; 115ERROR: invalid hexadecimal data: odd number of digits 116LINE 1: SELECT E'\\xDeAdBeE'::bytea; 117 ^ 118SELECT E'\\xDeAdBeEx'::bytea; 119ERROR: invalid hexadecimal digit: "x" 120LINE 1: SELECT E'\\xDeAdBeEx'::bytea; 121 ^ 122SELECT E'\\xDe00BeEf'::bytea; 123 bytea 124------------ 125 \xde00beef 126(1 row) 127 128SELECT E'DeAdBeEf'::bytea; 129 bytea 130-------------------- 131 \x4465416442654566 132(1 row) 133 134SELECT E'De\\000dBeEf'::bytea; 135 bytea 136-------------------- 137 \x4465006442654566 138(1 row) 139 140SELECT E'De\123dBeEf'::bytea; 141 bytea 142-------------------- 143 \x4465536442654566 144(1 row) 145 146SELECT E'De\\123dBeEf'::bytea; 147 bytea 148-------------------- 149 \x4465536442654566 150(1 row) 151 152SELECT E'De\\678dBeEf'::bytea; 153ERROR: invalid input syntax for type bytea 154LINE 1: SELECT E'De\\678dBeEf'::bytea; 155 ^ 156SET bytea_output TO escape; 157SELECT E'\\xDeAdBeEf'::bytea; 158 bytea 159------------------ 160 \336\255\276\357 161(1 row) 162 163SELECT E'\\x De Ad Be Ef '::bytea; 164 bytea 165------------------ 166 \336\255\276\357 167(1 row) 168 169SELECT E'\\xDe00BeEf'::bytea; 170 bytea 171------------------ 172 \336\000\276\357 173(1 row) 174 175SELECT E'DeAdBeEf'::bytea; 176 bytea 177---------- 178 DeAdBeEf 179(1 row) 180 181SELECT E'De\\000dBeEf'::bytea; 182 bytea 183------------- 184 De\000dBeEf 185(1 row) 186 187SELECT E'De\\123dBeEf'::bytea; 188 bytea 189---------- 190 DeSdBeEf 191(1 row) 192 193-- 194-- test conversions between various string types 195-- E021-10 implicit casting among the character data types 196-- 197SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL; 198 text(char) 199------------ 200 a 201 ab 202 abcd 203 abcd 204(4 rows) 205 206SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL; 207 text(varchar) 208--------------- 209 a 210 ab 211 abcd 212 abcd 213(4 rows) 214 215SELECT CAST(name 'namefield' AS text) AS "text(name)"; 216 text(name) 217------------ 218 namefield 219(1 row) 220 221-- since this is an explicit cast, it should truncate w/o error: 222SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL; 223 char(text) 224------------ 225 doh! 226 hi de ho n 227(2 rows) 228 229-- note: implicit-cast case is tested in char.sql 230SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL; 231 char(text) 232---------------------- 233 doh! 234 hi de ho neighbor 235(2 rows) 236 237SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL; 238 char(varchar) 239--------------- 240 a 241 ab 242 abcd 243 abcd 244(4 rows) 245 246SELECT CAST(name 'namefield' AS char(10)) AS "char(name)"; 247 char(name) 248------------ 249 namefield 250(1 row) 251 252SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL; 253 varchar(text) 254------------------- 255 doh! 256 hi de ho neighbor 257(2 rows) 258 259SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL; 260 varchar(char) 261--------------- 262 a 263 ab 264 abcd 265 abcd 266(4 rows) 267 268SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)"; 269 varchar(name) 270--------------- 271 namefield 272(1 row) 273 274-- 275-- test SQL string functions 276-- E### and T### are feature reference numbers from SQL99 277-- 278-- E021-09 trim function 279SELECT TRIM(BOTH FROM ' bunch o blanks ') = 'bunch o blanks' AS "bunch o blanks"; 280 bunch o blanks 281---------------- 282 t 283(1 row) 284 285SELECT TRIM(LEADING FROM ' bunch o blanks ') = 'bunch o blanks ' AS "bunch o blanks "; 286 bunch o blanks 287------------------ 288 t 289(1 row) 290 291SELECT TRIM(TRAILING FROM ' bunch o blanks ') = ' bunch o blanks' AS " bunch o blanks"; 292 bunch o blanks 293------------------ 294 t 295(1 row) 296 297SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs"; 298 some Xs 299--------- 300 t 301(1 row) 302 303-- E021-06 substring expression 304SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890"; 305 34567890 306---------- 307 t 308(1 row) 309 310SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456"; 311 456 312----- 313 t 314(1 row) 315 316-- T581 regular expression substring (with SQL99's bizarre regexp syntax) 317SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd"; 318 bcd 319----- 320 bcd 321(1 row) 322 323-- No match should return NULL 324SELECT SUBSTRING('abcdefg' FROM '#"(b_d)#"%' FOR '#') IS NULL AS "True"; 325 True 326------ 327 t 328(1 row) 329 330-- Null inputs should return NULL 331SELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True"; 332 True 333------ 334 t 335(1 row) 336 337SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True"; 338 True 339------ 340 t 341(1 row) 342 343SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True"; 344 True 345------ 346 t 347(1 row) 348 349-- PostgreSQL extension to allow omitting the escape character; 350-- here the regexp is taken as Posix syntax 351SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde"; 352 cde 353----- 354 cde 355(1 row) 356 357-- With a parenthesized subexpression, return only what matches the subexpr 358SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde"; 359 cde 360----- 361 cde 362(1 row) 363 364-- PostgreSQL extension to allow using back reference in replace string; 365SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3'); 366 regexp_replace 367---------------- 368 (111) 222-3333 369(1 row) 370 371SELECT regexp_replace('AAA BBB CCC ', E'\\s+', ' ', 'g'); 372 regexp_replace 373---------------- 374 AAA BBB CCC 375(1 row) 376 377SELECT regexp_replace('AAA', '^|$', 'Z', 'g'); 378 regexp_replace 379---------------- 380 ZAAAZ 381(1 row) 382 383SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi'); 384 regexp_replace 385---------------- 386 Z Z 387(1 row) 388 389-- invalid regexp option 390SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z'); 391ERROR: invalid regexp option: "z" 392-- set so we can tell NULL from empty string 393\pset null '\\N' 394-- return all matches from regexp 395SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$); 396 regexp_matches 397---------------- 398 {bar,beque} 399(1 row) 400 401-- test case insensitive 402SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i'); 403 regexp_matches 404---------------- 405 {bAR,bEqUE} 406(1 row) 407 408-- global option - more than one match 409SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g'); 410 regexp_matches 411---------------- 412 {bar,beque} 413 {bazil,barf} 414(2 rows) 415 416-- empty capture group (matched empty string) 417SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$); 418 regexp_matches 419---------------- 420 {bar,"",beque} 421(1 row) 422 423-- no match 424SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$); 425 regexp_matches 426---------------- 427(0 rows) 428 429-- optional capture group did not match, null entry in array 430SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$); 431 regexp_matches 432------------------ 433 {bar,NULL,beque} 434(1 row) 435 436-- no capture groups 437SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$); 438 regexp_matches 439---------------- 440 {barbeque} 441(1 row) 442 443-- start/end-of-line matches are of zero length 444SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '^', 'mg'); 445 regexp_matches 446---------------- 447 {""} 448 {""} 449 {""} 450 {""} 451(4 rows) 452 453SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '$', 'mg'); 454 regexp_matches 455---------------- 456 {""} 457 {""} 458 {""} 459 {""} 460(4 rows) 461 462SELECT regexp_matches('1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '^.?', 'mg'); 463 regexp_matches 464---------------- 465 {1} 466 {2} 467 {3} 468 {4} 469 {""} 470(5 rows) 471 472SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '.?$', 'mg'); 473 regexp_matches 474---------------- 475 {""} 476 {1} 477 {""} 478 {2} 479 {""} 480 {3} 481 {""} 482 {4} 483 {""} 484 {""} 485(10 rows) 486 487SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4', '.?$', 'mg'); 488 regexp_matches 489---------------- 490 {""} 491 {1} 492 {""} 493 {2} 494 {""} 495 {3} 496 {""} 497 {4} 498 {""} 499(9 rows) 500 501-- give me errors 502SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz'); 503ERROR: invalid regexp option: "z" 504SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$); 505ERROR: invalid regular expression: parentheses () not balanced 506SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$); 507ERROR: invalid regular expression: invalid repetition count(s) 508-- split string on regexp 509SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo; 510 foo | length 511-------+-------- 512 the | 3 513 quick | 5 514 brown | 5 515 fox | 3 516 jumps | 5 517 over | 4 518 the | 3 519 lazy | 4 520 dog | 3 521(9 rows) 522 523SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$); 524 regexp_split_to_array 525----------------------------------------------- 526 {the,quick,brown,fox,jumps,over,the,lazy,dog} 527(1 row) 528 529SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo; 530 foo | length 531-----+-------- 532 t | 1 533 h | 1 534 e | 1 535 q | 1 536 u | 1 537 i | 1 538 c | 1 539 k | 1 540 b | 1 541 r | 1 542 o | 1 543 w | 1 544 n | 1 545 f | 1 546 o | 1 547 x | 1 548 j | 1 549 u | 1 550 m | 1 551 p | 1 552 s | 1 553 o | 1 554 v | 1 555 e | 1 556 r | 1 557 t | 1 558 h | 1 559 e | 1 560 l | 1 561 a | 1 562 z | 1 563 y | 1 564 d | 1 565 o | 1 566 g | 1 567(35 rows) 568 569SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$); 570 regexp_split_to_array 571------------------------------------------------------------------------- 572 {t,h,e,q,u,i,c,k,b,r,o,w,n,f,o,x,j,u,m,p,s,o,v,e,r,t,h,e,l,a,z,y,d,o,g} 573(1 row) 574 575SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo; 576 foo | length 577-----+-------- 578 t | 1 579 h | 1 580 e | 1 581 | 1 582 q | 1 583 u | 1 584 i | 1 585 c | 1 586 k | 1 587 | 1 588 b | 1 589 r | 1 590 o | 1 591 w | 1 592 n | 1 593 | 1 594 f | 1 595 o | 1 596 x | 1 597 | 1 598 j | 1 599 u | 1 600 m | 1 601 p | 1 602 s | 1 603 | 1 604 o | 1 605 v | 1 606 e | 1 607 r | 1 608 | 1 609 t | 1 610 h | 1 611 e | 1 612 | 1 613 l | 1 614 a | 1 615 z | 1 616 y | 1 617 | 1 618 d | 1 619 o | 1 620 g | 1 621(43 rows) 622 623SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', ''); 624 regexp_split_to_array 625--------------------------------------------------------------------------------------------------------- 626 {t,h,e," ",q,u,i,c,k," ",b,r,o,w,n," ",f,o,x," ",j,u,m,p,s," ",o,v,e,r," ",t,h,e," ",l,a,z,y," ",d,o,g} 627(1 row) 628 629-- case insensitive 630SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo; 631 foo | length 632---------------------------+-------- 633 th | 2 634 QUick bROWn FOx jUMPs ov | 25 635 r Th | 4 636 lazy dOG | 9 637(4 rows) 638 639SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i'); 640 regexp_split_to_array 641----------------------------------------------------- 642 {th," QUick bROWn FOx jUMPs ov","r Th"," lazy dOG"} 643(1 row) 644 645-- no match of pattern 646SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo; 647 foo | length 648---------------------------------------------+-------- 649 the quick brown fox jumps over the lazy dog | 43 650(1 row) 651 652SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch'); 653 regexp_split_to_array 654------------------------------------------------- 655 {"the quick brown fox jumps over the lazy dog"} 656(1 row) 657 658-- some corner cases 659SELECT regexp_split_to_array('123456','1'); 660 regexp_split_to_array 661----------------------- 662 {"",23456} 663(1 row) 664 665SELECT regexp_split_to_array('123456','6'); 666 regexp_split_to_array 667----------------------- 668 {12345,""} 669(1 row) 670 671SELECT regexp_split_to_array('123456','.'); 672 regexp_split_to_array 673------------------------ 674 {"","","","","","",""} 675(1 row) 676 677SELECT regexp_split_to_array('123456',''); 678 regexp_split_to_array 679----------------------- 680 {1,2,3,4,5,6} 681(1 row) 682 683SELECT regexp_split_to_array('123456','(?:)'); 684 regexp_split_to_array 685----------------------- 686 {1,2,3,4,5,6} 687(1 row) 688 689SELECT regexp_split_to_array('1',''); 690 regexp_split_to_array 691----------------------- 692 {1} 693(1 row) 694 695-- errors 696SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo; 697ERROR: invalid regexp option: "z" 698SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz'); 699ERROR: invalid regexp option: "z" 700-- global option meaningless for regexp_split 701SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo; 702ERROR: regexp_split_to_table does not support the global option 703SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g'); 704ERROR: regexp_split_to_array does not support the global option 705-- change NULL-display back 706\pset null '' 707-- E021-11 position expression 708SELECT POSITION('4' IN '1234567890') = '4' AS "4"; 709 4 710--- 711 t 712(1 row) 713 714SELECT POSITION('5' IN '1234567890') = '5' AS "5"; 715 5 716--- 717 t 718(1 row) 719 720-- T312 character overlay function 721SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f"; 722 abc45f 723-------- 724 abc45f 725(1 row) 726 727SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba"; 728 yabadaba 729---------- 730 yabadaba 731(1 row) 732 733SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo"; 734 yabadabadoo 735------------- 736 yabadabadoo 737(1 row) 738 739SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba"; 740 bubba 741------- 742 bubba 743(1 row) 744 745-- 746-- test LIKE 747-- Be sure to form every test as a LIKE/NOT LIKE pair. 748-- 749-- simplest examples 750-- E061-04 like predicate 751SELECT 'hawkeye' LIKE 'h%' AS "true"; 752 true 753------ 754 t 755(1 row) 756 757SELECT 'hawkeye' NOT LIKE 'h%' AS "false"; 758 false 759------- 760 f 761(1 row) 762 763SELECT 'hawkeye' LIKE 'H%' AS "false"; 764 false 765------- 766 f 767(1 row) 768 769SELECT 'hawkeye' NOT LIKE 'H%' AS "true"; 770 true 771------ 772 t 773(1 row) 774 775SELECT 'hawkeye' LIKE 'indio%' AS "false"; 776 false 777------- 778 f 779(1 row) 780 781SELECT 'hawkeye' NOT LIKE 'indio%' AS "true"; 782 true 783------ 784 t 785(1 row) 786 787SELECT 'hawkeye' LIKE 'h%eye' AS "true"; 788 true 789------ 790 t 791(1 row) 792 793SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false"; 794 false 795------- 796 f 797(1 row) 798 799SELECT 'indio' LIKE '_ndio' AS "true"; 800 true 801------ 802 t 803(1 row) 804 805SELECT 'indio' NOT LIKE '_ndio' AS "false"; 806 false 807------- 808 f 809(1 row) 810 811SELECT 'indio' LIKE 'in__o' AS "true"; 812 true 813------ 814 t 815(1 row) 816 817SELECT 'indio' NOT LIKE 'in__o' AS "false"; 818 false 819------- 820 f 821(1 row) 822 823SELECT 'indio' LIKE 'in_o' AS "false"; 824 false 825------- 826 f 827(1 row) 828 829SELECT 'indio' NOT LIKE 'in_o' AS "true"; 830 true 831------ 832 t 833(1 row) 834 835-- unused escape character 836SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true"; 837 true 838------ 839 t 840(1 row) 841 842SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false"; 843 false 844------- 845 f 846(1 row) 847 848SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true"; 849 true 850------ 851 t 852(1 row) 853 854SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false"; 855 false 856------- 857 f 858(1 row) 859 860-- escape character 861-- E061-05 like predicate with escape clause 862SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true"; 863 true 864------ 865 t 866(1 row) 867 868SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false"; 869 false 870------- 871 f 872(1 row) 873 874SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false"; 875 false 876------- 877 f 878(1 row) 879 880SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true"; 881 true 882------ 883 t 884(1 row) 885 886SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true"; 887 true 888------ 889 t 890(1 row) 891 892SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false"; 893 false 894------- 895 f 896(1 row) 897 898SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true"; 899 true 900------ 901 t 902(1 row) 903 904SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false"; 905 false 906------- 907 f 908(1 row) 909 910SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true"; 911 true 912------ 913 t 914(1 row) 915 916SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false"; 917 false 918------- 919 f 920(1 row) 921 922SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true"; 923 true 924------ 925 t 926(1 row) 927 928SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false"; 929 false 930------- 931 f 932(1 row) 933 934SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false"; 935 false 936------- 937 f 938(1 row) 939 940SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true"; 941 true 942------ 943 t 944(1 row) 945 946SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true"; 947 true 948------ 949 t 950(1 row) 951 952SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false"; 953 false 954------- 955 f 956(1 row) 957 958-- escape character same as pattern character 959SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true"; 960 true 961------ 962 t 963(1 row) 964 965SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false"; 966 false 967------- 968 f 969(1 row) 970 971SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true"; 972 true 973------ 974 t 975(1 row) 976 977SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false"; 978 false 979------- 980 f 981(1 row) 982 983SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true"; 984 true 985------ 986 t 987(1 row) 988 989SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false"; 990 false 991------- 992 f 993(1 row) 994 995SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true"; 996 true 997------ 998 t 999(1 row) 1000 1001SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false"; 1002 false 1003------- 1004 f 1005(1 row) 1006 1007SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false"; 1008 false 1009------- 1010 f 1011(1 row) 1012 1013SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true"; 1014 true 1015------ 1016 t 1017(1 row) 1018 1019-- 1020-- test ILIKE (case-insensitive LIKE) 1021-- Be sure to form every test as an ILIKE/NOT ILIKE pair. 1022-- 1023SELECT 'hawkeye' ILIKE 'h%' AS "true"; 1024 true 1025------ 1026 t 1027(1 row) 1028 1029SELECT 'hawkeye' NOT ILIKE 'h%' AS "false"; 1030 false 1031------- 1032 f 1033(1 row) 1034 1035SELECT 'hawkeye' ILIKE 'H%' AS "true"; 1036 true 1037------ 1038 t 1039(1 row) 1040 1041SELECT 'hawkeye' NOT ILIKE 'H%' AS "false"; 1042 false 1043------- 1044 f 1045(1 row) 1046 1047SELECT 'hawkeye' ILIKE 'H%Eye' AS "true"; 1048 true 1049------ 1050 t 1051(1 row) 1052 1053SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false"; 1054 false 1055------- 1056 f 1057(1 row) 1058 1059SELECT 'Hawkeye' ILIKE 'h%' AS "true"; 1060 true 1061------ 1062 t 1063(1 row) 1064 1065SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false"; 1066 false 1067------- 1068 f 1069(1 row) 1070 1071-- 1072-- test %/_ combination cases, cf bugs #4821 and #5478 1073-- 1074SELECT 'foo' LIKE '_%' as t, 'f' LIKE '_%' as t, '' LIKE '_%' as f; 1075 t | t | f 1076---+---+--- 1077 t | t | f 1078(1 row) 1079 1080SELECT 'foo' LIKE '%_' as t, 'f' LIKE '%_' as t, '' LIKE '%_' as f; 1081 t | t | f 1082---+---+--- 1083 t | t | f 1084(1 row) 1085 1086SELECT 'foo' LIKE '__%' as t, 'foo' LIKE '___%' as t, 'foo' LIKE '____%' as f; 1087 t | t | f 1088---+---+--- 1089 t | t | f 1090(1 row) 1091 1092SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f; 1093 t | t | f 1094---+---+--- 1095 t | t | f 1096(1 row) 1097 1098SELECT 'jack' LIKE '%____%' AS t; 1099 t 1100--- 1101 t 1102(1 row) 1103 1104-- 1105-- test implicit type conversion 1106-- 1107-- E021-07 character concatenation 1108SELECT 'unknown' || ' and unknown' AS "Concat unknown types"; 1109 Concat unknown types 1110---------------------- 1111 unknown and unknown 1112(1 row) 1113 1114SELECT text 'text' || ' and unknown' AS "Concat text to unknown type"; 1115 Concat text to unknown type 1116----------------------------- 1117 text and unknown 1118(1 row) 1119 1120SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type"; 1121 Concat char to unknown type 1122----------------------------- 1123 characters and text 1124(1 row) 1125 1126SELECT text 'text' || char(20) ' and characters' AS "Concat text to char"; 1127 Concat text to char 1128--------------------- 1129 text and characters 1130(1 row) 1131 1132SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar"; 1133 Concat text to varchar 1134------------------------ 1135 text and varchar 1136(1 row) 1137 1138-- 1139-- test substr with toasted text values 1140-- 1141CREATE TABLE toasttest(f1 text); 1142insert into toasttest values(repeat('1234567890',10000)); 1143insert into toasttest values(repeat('1234567890',10000)); 1144-- 1145-- Ensure that some values are uncompressed, to test the faster substring 1146-- operation used in that case 1147-- 1148alter table toasttest alter column f1 set storage external; 1149insert into toasttest values(repeat('1234567890',10000)); 1150insert into toasttest values(repeat('1234567890',10000)); 1151-- If the starting position is zero or less, then return from the start of the string 1152-- adjusting the length to be consistent with the "negative start" per SQL. 1153SELECT substr(f1, -1, 5) from toasttest; 1154 substr 1155-------- 1156 123 1157 123 1158 123 1159 123 1160(4 rows) 1161 1162-- If the length is less than zero, an ERROR is thrown. 1163SELECT substr(f1, 5, -1) from toasttest; 1164ERROR: negative substring length not allowed 1165-- If no third argument (length) is provided, the length to the end of the 1166-- string is assumed. 1167SELECT substr(f1, 99995) from toasttest; 1168 substr 1169-------- 1170 567890 1171 567890 1172 567890 1173 567890 1174(4 rows) 1175 1176-- If start plus length is > string length, the result is truncated to 1177-- string length 1178SELECT substr(f1, 99995, 10) from toasttest; 1179 substr 1180-------- 1181 567890 1182 567890 1183 567890 1184 567890 1185(4 rows) 1186 1187DROP TABLE toasttest; 1188-- 1189-- test substr with toasted bytea values 1190-- 1191CREATE TABLE toasttest(f1 bytea); 1192insert into toasttest values(decode(repeat('1234567890',10000),'escape')); 1193insert into toasttest values(decode(repeat('1234567890',10000),'escape')); 1194-- 1195-- Ensure that some values are uncompressed, to test the faster substring 1196-- operation used in that case 1197-- 1198alter table toasttest alter column f1 set storage external; 1199insert into toasttest values(decode(repeat('1234567890',10000),'escape')); 1200insert into toasttest values(decode(repeat('1234567890',10000),'escape')); 1201-- If the starting position is zero or less, then return from the start of the string 1202-- adjusting the length to be consistent with the "negative start" per SQL. 1203SELECT substr(f1, -1, 5) from toasttest; 1204 substr 1205-------- 1206 123 1207 123 1208 123 1209 123 1210(4 rows) 1211 1212-- If the length is less than zero, an ERROR is thrown. 1213SELECT substr(f1, 5, -1) from toasttest; 1214ERROR: negative substring length not allowed 1215-- If no third argument (length) is provided, the length to the end of the 1216-- string is assumed. 1217SELECT substr(f1, 99995) from toasttest; 1218 substr 1219-------- 1220 567890 1221 567890 1222 567890 1223 567890 1224(4 rows) 1225 1226-- If start plus length is > string length, the result is truncated to 1227-- string length 1228SELECT substr(f1, 99995, 10) from toasttest; 1229 substr 1230-------- 1231 567890 1232 567890 1233 567890 1234 567890 1235(4 rows) 1236 1237DROP TABLE toasttest; 1238-- test internally compressing datums 1239-- this tests compressing a datum to a very small size which exercises a 1240-- corner case in packed-varlena handling: even though small, the compressed 1241-- datum must be given a 4-byte header because there are no bits to indicate 1242-- compression in a 1-byte header 1243CREATE TABLE toasttest (c char(4096)); 1244INSERT INTO toasttest VALUES('x'); 1245SELECT length(c), c::text FROM toasttest; 1246 length | c 1247--------+--- 1248 1 | x 1249(1 row) 1250 1251SELECT c FROM toasttest; 1252 c 1253------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 1254 x 1255(1 row) 1256 1257DROP TABLE toasttest; 1258-- 1259-- test length 1260-- 1261SELECT length('abcdef') AS "length_6"; 1262 length_6 1263---------- 1264 6 1265(1 row) 1266 1267-- 1268-- test strpos 1269-- 1270SELECT strpos('abcdef', 'cd') AS "pos_3"; 1271 pos_3 1272------- 1273 3 1274(1 row) 1275 1276SELECT strpos('abcdef', 'xy') AS "pos_0"; 1277 pos_0 1278------- 1279 0 1280(1 row) 1281 1282-- 1283-- test replace 1284-- 1285SELECT replace('abcdef', 'de', '45') AS "abc45f"; 1286 abc45f 1287-------- 1288 abc45f 1289(1 row) 1290 1291SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo"; 1292 ya123da123doo 1293--------------- 1294 ya123da123doo 1295(1 row) 1296 1297SELECT replace('yabadoo', 'bad', '') AS "yaoo"; 1298 yaoo 1299------ 1300 yaoo 1301(1 row) 1302 1303-- 1304-- test split_part 1305-- 1306select split_part('joeuser@mydatabase','@',0) AS "an error"; 1307ERROR: field position must be greater than zero 1308select split_part('joeuser@mydatabase','@',1) AS "joeuser"; 1309 joeuser 1310--------- 1311 joeuser 1312(1 row) 1313 1314select split_part('joeuser@mydatabase','@',2) AS "mydatabase"; 1315 mydatabase 1316------------ 1317 mydatabase 1318(1 row) 1319 1320select split_part('joeuser@mydatabase','@',3) AS "empty string"; 1321 empty string 1322-------------- 1323 1324(1 row) 1325 1326select split_part('@joeuser@mydatabase@','@',2) AS "joeuser"; 1327 joeuser 1328--------- 1329 joeuser 1330(1 row) 1331 1332-- 1333-- test to_hex 1334-- 1335select to_hex(256*256*256 - 1) AS "ffffff"; 1336 ffffff 1337-------- 1338 ffffff 1339(1 row) 1340 1341select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"; 1342 ffffffff 1343---------- 1344 ffffffff 1345(1 row) 1346 1347-- 1348-- MD5 test suite - from IETF RFC 1321 1349-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt) 1350-- 1351select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE"; 1352 TRUE 1353------ 1354 t 1355(1 row) 1356 1357select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE"; 1358 TRUE 1359------ 1360 t 1361(1 row) 1362 1363select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE"; 1364 TRUE 1365------ 1366 t 1367(1 row) 1368 1369select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE"; 1370 TRUE 1371------ 1372 t 1373(1 row) 1374 1375select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE"; 1376 TRUE 1377------ 1378 t 1379(1 row) 1380 1381select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE"; 1382 TRUE 1383------ 1384 t 1385(1 row) 1386 1387select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE"; 1388 TRUE 1389------ 1390 t 1391(1 row) 1392 1393select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE"; 1394 TRUE 1395------ 1396 t 1397(1 row) 1398 1399select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE"; 1400 TRUE 1401------ 1402 t 1403(1 row) 1404 1405select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE"; 1406 TRUE 1407------ 1408 t 1409(1 row) 1410 1411select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE"; 1412 TRUE 1413------ 1414 t 1415(1 row) 1416 1417select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE"; 1418 TRUE 1419------ 1420 t 1421(1 row) 1422 1423select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE"; 1424 TRUE 1425------ 1426 t 1427(1 row) 1428 1429select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE"; 1430 TRUE 1431------ 1432 t 1433(1 row) 1434 1435-- 1436-- encode/decode 1437-- 1438SET bytea_output TO hex; 1439SELECT encode('\x1234567890abcdef00', 'hex'); 1440 encode 1441-------------------- 1442 1234567890abcdef00 1443(1 row) 1444 1445SELECT decode('1234567890abcdef00', 'hex'); 1446 decode 1447---------------------- 1448 \x1234567890abcdef00 1449(1 row) 1450 1451SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64'); 1452 encode 1453------------------------------------------------------------------------------ 1454 EjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN7wABEjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN+ 1455 7wABEjRWeJCrze8AAQ== 1456(1 row) 1457 1458SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 1459 'base64'), 'base64'); 1460 decode 1461------------------------------------------------------------------------------------------------------------------------------------------------ 1462 \x1234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef0001 1463(1 row) 1464 1465SELECT encode('\x1234567890abcdef00', 'escape'); 1466 encode 1467----------------------------- 1468 \x124Vx\220\253\315\357\000 1469(1 row) 1470 1471SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape'); 1472 decode 1473---------------------- 1474 \x1234567890abcdef00 1475(1 row) 1476 1477-- 1478-- get_bit/set_bit etc 1479-- 1480SELECT get_bit('\x1234567890abcdef00'::bytea, 43); 1481 get_bit 1482--------- 1483 1 1484(1 row) 1485 1486SELECT get_bit('\x1234567890abcdef00'::bytea, 99); -- error 1487ERROR: index 99 out of valid range, 0..71 1488SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0); 1489 set_bit 1490---------------------- 1491 \x1234567890a3cdef00 1492(1 row) 1493 1494SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0); -- error 1495ERROR: index 99 out of valid range, 0..71 1496SELECT get_byte('\x1234567890abcdef00'::bytea, 3); 1497 get_byte 1498---------- 1499 120 1500(1 row) 1501 1502SELECT get_byte('\x1234567890abcdef00'::bytea, 99); -- error 1503ERROR: index 99 out of valid range, 0..8 1504SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11); 1505 set_byte 1506---------------------- 1507 \x1234567890abcd0b00 1508(1 row) 1509 1510SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11); -- error 1511ERROR: index 99 out of valid range, 0..8 1512-- 1513-- test behavior of escape_string_warning and standard_conforming_strings options 1514-- 1515set escape_string_warning = off; 1516set standard_conforming_strings = off; 1517show escape_string_warning; 1518 escape_string_warning 1519----------------------- 1520 off 1521(1 row) 1522 1523show standard_conforming_strings; 1524 standard_conforming_strings 1525----------------------------- 1526 off 1527(1 row) 1528 1529set escape_string_warning = on; 1530set standard_conforming_strings = on; 1531show escape_string_warning; 1532 escape_string_warning 1533----------------------- 1534 on 1535(1 row) 1536 1537show standard_conforming_strings; 1538 standard_conforming_strings 1539----------------------------- 1540 on 1541(1 row) 1542 1543select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6; 1544 f1 | f2 | f3 | f4 | f5 | f6 1545-------+--------+---------+-------+--------+---- 1546 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\ 1547(1 row) 1548 1549set standard_conforming_strings = off; 1550select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as f6; 1551WARNING: nonstandard use of \\ in a string literal 1552LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,... 1553 ^ 1554HINT: Use the escape string syntax for backslashes, e.g., E'\\'. 1555WARNING: nonstandard use of \\ in a string literal 1556LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,... 1557 ^ 1558HINT: Use the escape string syntax for backslashes, e.g., E'\\'. 1559WARNING: nonstandard use of \\ in a string literal 1560LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,... 1561 ^ 1562HINT: Use the escape string syntax for backslashes, e.g., E'\\'. 1563WARNING: nonstandard use of \\ in a string literal 1564LINE 1: ...bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' ... 1565 ^ 1566HINT: Use the escape string syntax for backslashes, e.g., E'\\'. 1567WARNING: nonstandard use of \\ in a string literal 1568LINE 1: ...'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd'... 1569 ^ 1570HINT: Use the escape string syntax for backslashes, e.g., E'\\'. 1571WARNING: nonstandard use of \\ in a string literal 1572LINE 1: ...'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as ... 1573 ^ 1574HINT: Use the escape string syntax for backslashes, e.g., E'\\'. 1575 f1 | f2 | f3 | f4 | f5 | f6 1576-------+--------+---------+-------+--------+---- 1577 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\ 1578(1 row) 1579 1580set escape_string_warning = off; 1581set standard_conforming_strings = on; 1582select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6; 1583 f1 | f2 | f3 | f4 | f5 | f6 1584-------+--------+---------+-------+--------+---- 1585 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\ 1586(1 row) 1587 1588set standard_conforming_strings = off; 1589select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as f6; 1590 f1 | f2 | f3 | f4 | f5 | f6 1591-------+--------+---------+-------+--------+---- 1592 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\ 1593(1 row) 1594 1595-- 1596-- Additional string functions 1597-- 1598SET bytea_output TO escape; 1599SELECT initcap('hi THOMAS'); 1600 initcap 1601----------- 1602 Hi Thomas 1603(1 row) 1604 1605SELECT lpad('hi', 5, 'xy'); 1606 lpad 1607------- 1608 xyxhi 1609(1 row) 1610 1611SELECT lpad('hi', 5); 1612 lpad 1613------- 1614 hi 1615(1 row) 1616 1617SELECT lpad('hi', -5, 'xy'); 1618 lpad 1619------ 1620 1621(1 row) 1622 1623SELECT lpad('hello', 2); 1624 lpad 1625------ 1626 he 1627(1 row) 1628 1629SELECT lpad('hi', 5, ''); 1630 lpad 1631------ 1632 hi 1633(1 row) 1634 1635SELECT rpad('hi', 5, 'xy'); 1636 rpad 1637------- 1638 hixyx 1639(1 row) 1640 1641SELECT rpad('hi', 5); 1642 rpad 1643------- 1644 hi 1645(1 row) 1646 1647SELECT rpad('hi', -5, 'xy'); 1648 rpad 1649------ 1650 1651(1 row) 1652 1653SELECT rpad('hello', 2); 1654 rpad 1655------ 1656 he 1657(1 row) 1658 1659SELECT rpad('hi', 5, ''); 1660 rpad 1661------ 1662 hi 1663(1 row) 1664 1665SELECT ltrim('zzzytrim', 'xyz'); 1666 ltrim 1667------- 1668 trim 1669(1 row) 1670 1671SELECT translate('', '14', 'ax'); 1672 translate 1673----------- 1674 1675(1 row) 1676 1677SELECT translate('12345', '14', 'ax'); 1678 translate 1679----------- 1680 a23x5 1681(1 row) 1682 1683SELECT ascii('x'); 1684 ascii 1685------- 1686 120 1687(1 row) 1688 1689SELECT ascii(''); 1690 ascii 1691------- 1692 0 1693(1 row) 1694 1695SELECT chr(65); 1696 chr 1697----- 1698 A 1699(1 row) 1700 1701SELECT chr(0); 1702ERROR: null character not permitted 1703SELECT repeat('Pg', 4); 1704 repeat 1705---------- 1706 PgPgPgPg 1707(1 row) 1708 1709SELECT repeat('Pg', -4); 1710 repeat 1711-------- 1712 1713(1 row) 1714 1715SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea); 1716 btrim 1717------- 1718 Tom 1719(1 row) 1720 1721SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea); 1722 btrim 1723------- 1724 trim 1725(1 row) 1726 1727SELECT btrim(''::bytea, E'\\000'::bytea); 1728 btrim 1729------- 1730 1731(1 row) 1732 1733SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea); 1734 btrim 1735-------------- 1736 \000trim\000 1737(1 row) 1738 1739SELECT encode(overlay(E'Th\\000omas'::bytea placing E'Th\\001omas'::bytea from 2),'escape'); 1740 encode 1741------------- 1742 TTh\x01omas 1743(1 row) 1744 1745SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 8),'escape'); 1746 encode 1747-------------------- 1748 Th\000omas\x02\x03 1749(1 row) 1750 1751SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 5 for 3),'escape'); 1752 encode 1753----------------- 1754 Th\000o\x02\x03 1755(1 row) 1756 1757