text.sql 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /*
  11. * various string functions
  12. */
  13. select concat('one');
  14. select concat_ws('#','one');
  15. select concat_ws(',',10,20,null,30);
  16. select concat_ws('',10,20,null,30);
  17. select concat_ws(NULL,10,20,null,30) is null;
  18. select reverse('abcde');
  19. select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i;
  20. select quote_literal('');
  21. select quote_literal('abc''');
  22. select quote_literal(e'\\');
  23. /*
  24. * format
  25. */
  26. select format(NULL);
  27. select format('Hello');
  28. select format('Hello %s', 'World');
  29. select format('Hello %%');
  30. select format('Hello %%%%');
  31. -- should fail
  32. select format('Hello %s %s', 'World');
  33. select format('Hello %s');
  34. select format('Hello %x', 20);
  35. -- check literal and sql identifiers
  36. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello');
  37. select format('%s%s%s','Hello', NULL,'World');
  38. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, NULL);
  39. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', NULL, 'Hello');
  40. -- should fail, sql identifier cannot be NULL
  41. select format('INSERT INTO %I VALUES(%L,%L)', NULL, 10, 'Hello');
  42. -- check positional placeholders
  43. select format('%1$s %3$s', 1, 2, 3);
  44. select format('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  45. -- should fail
  46. select format('%1$s %4$s', 1, 2, 3);
  47. select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  48. select format('%0$s', 'Hello');
  49. select format('%*0$s', 'Hello');
  50. select format('%1$', 1);
  51. select format('%1$1', 1);
  52. -- check mix of positional and ordered placeholders
  53. select format('Hello %s %1$s %s', 'World', 'Hello again');
  54. select format('Hello %s %s, %2$s %2$s', 'World', 'Hello again');
  55. -- check field widths and left, right alignment
  56. select format('>>%10s<<', 'Hello');
  57. select format('>>%10s<<', NULL);
  58. select format('>>%10s<<', '');
  59. select format('>>%-10s<<', '');
  60. select format('>>%-10s<<', 'Hello');
  61. select format('>>%-10s<<', NULL);
  62. select format('>>%1$10s<<', 'Hello');
  63. select format('>>%1$-10I<<', 'Hello');
  64. select format('>>%2$*1$L<<', 10, 'Hello');
  65. select format('>>%2$*1$L<<', 10, NULL);
  66. select format('>>%2$*1$L<<', -10, NULL);
  67. select format('>>%*s<<', 10, 'Hello');
  68. select format('>>%*1$s<<', 10, 'Hello');
  69. select format('>>%-s<<', 'Hello');
  70. select format('>>%10L<<', NULL);
  71. select format('>>%2$*1$L<<', NULL, 'Hello');
  72. select format('>>%2$*1$L<<', 0, 'Hello');