text.sql 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. --
  2. -- TEXT
  3. --
  4. SELECT text 'this is a text string' = text 'this is a text string' AS true;
  5. SELECT text 'this is a text string' = text 'this is a text strin' AS false;
  6. CREATE TABLE TEXT_TBL (f1 text);
  7. INSERT INTO TEXT_TBL VALUES ('doh!');
  8. INSERT INTO TEXT_TBL VALUES ('hi de ho neighbor');
  9. SELECT * FROM TEXT_TBL;
  10. -- As of 8.3 we have removed most implicit casts to text, so that for example
  11. -- this no longer works:
  12. select length(42);
  13. -- But as a special exception for usability's sake, we still allow implicit
  14. -- casting to text in concatenations, so long as the other input is text or
  15. -- an unknown literal. So these work:
  16. select 'four: '::text || 2+2;
  17. select 'four: ' || 2+2;
  18. -- but not this:
  19. select 3 || 4.0;
  20. /*
  21. * various string functions
  22. */
  23. select concat('one');
  24. select concat(1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
  25. select concat_ws('#','one');
  26. select concat_ws('#',1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
  27. select concat_ws(',',10,20,null,30);
  28. select concat_ws('',10,20,null,30);
  29. select concat_ws(NULL,10,20,null,30) is null;
  30. select reverse('abcde');
  31. select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i;
  32. select quote_literal('');
  33. select quote_literal('abc''');
  34. select quote_literal(e'\\');
  35. -- check variadic labeled argument
  36. select concat(variadic array[1,2,3]);
  37. select concat_ws(',', variadic array[1,2,3]);
  38. select concat_ws(',', variadic NULL::int[]);
  39. select concat(variadic NULL::int[]) is NULL;
  40. select concat(variadic '{}'::int[]) = '';
  41. --should fail
  42. select concat_ws(',', variadic 10);
  43. /*
  44. * format
  45. */
  46. select format(NULL);
  47. select format('Hello');
  48. select format('Hello %s', 'World');
  49. select format('Hello %%');
  50. select format('Hello %%%%');
  51. -- should fail
  52. select format('Hello %s %s', 'World');
  53. select format('Hello %s');
  54. select format('Hello %x', 20);
  55. -- check literal and sql identifiers
  56. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello');
  57. select format('%s%s%s','Hello', NULL,'World');
  58. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, NULL);
  59. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', NULL, 'Hello');
  60. -- should fail, sql identifier cannot be NULL
  61. select format('INSERT INTO %I VALUES(%L,%L)', NULL, 10, 'Hello');
  62. -- check positional placeholders
  63. select format('%1$s %3$s', 1, 2, 3);
  64. select format('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  65. -- should fail
  66. select format('%1$s %4$s', 1, 2, 3);
  67. select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  68. select format('%0$s', 'Hello');
  69. select format('%*0$s', 'Hello');
  70. select format('%1$', 1);
  71. select format('%1$1', 1);
  72. -- check mix of positional and ordered placeholders
  73. select format('Hello %s %1$s %s', 'World', 'Hello again');
  74. select format('Hello %s %s, %2$s %2$s', 'World', 'Hello again');
  75. -- check variadic labeled arguments
  76. select format('%s, %s', variadic array['Hello','World']);
  77. select format('%s, %s', variadic array[1, 2]);
  78. select format('%s, %s', variadic array[true, false]);
  79. select format('%s, %s', variadic array[true, false]::text[]);
  80. -- check variadic with positional placeholders
  81. select format('%2$s, %1$s', variadic array['first', 'second']);
  82. select format('%2$s, %1$s', variadic array[1, 2]);
  83. -- variadic argument can be array type NULL, but should not be referenced
  84. select format('Hello', variadic NULL::int[]);
  85. -- variadic argument allows simulating more than FUNC_MAX_ARGS parameters
  86. select format(string_agg('%s',','), variadic array_agg(i))
  87. from generate_series(1,200) g(i);
  88. -- check field widths and left, right alignment
  89. select format('>>%10s<<', 'Hello');
  90. select format('>>%10s<<', NULL);
  91. select format('>>%10s<<', '');
  92. select format('>>%-10s<<', '');
  93. select format('>>%-10s<<', 'Hello');
  94. select format('>>%-10s<<', NULL);
  95. select format('>>%1$10s<<', 'Hello');
  96. select format('>>%1$-10I<<', 'Hello');
  97. select format('>>%2$*1$L<<', 10, 'Hello');
  98. select format('>>%2$*1$L<<', 10, NULL);
  99. select format('>>%2$*1$L<<', -10, NULL);
  100. select format('>>%*s<<', 10, 'Hello');
  101. select format('>>%*1$s<<', 10, 'Hello');
  102. select format('>>%-s<<', 'Hello');
  103. select format('>>%10L<<', NULL);
  104. select format('>>%2$*1$L<<', NULL, 'Hello');
  105. select format('>>%2$*1$L<<', 0, 'Hello');