text.out 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. --
  2. -- TEXT
  3. --
  4. SELECT text 'this is a text string' = text 'this is a text string' AS true;
  5. true
  6. ------
  7. t
  8. (1 row)
  9. SELECT text 'this is a text string' = text 'this is a text strin' AS false;
  10. false
  11. -------
  12. f
  13. (1 row)
  14. CREATE TABLE TEXT_TBL (f1 text);
  15. INSERT INTO TEXT_TBL VALUES ('doh!');
  16. INSERT INTO TEXT_TBL VALUES ('hi de ho neighbor');
  17. SELECT * FROM TEXT_TBL;
  18. f1
  19. -------------------
  20. doh!
  21. hi de ho neighbor
  22. (2 rows)
  23. /*
  24. * various string functions
  25. */
  26. select concat('one');
  27. concat
  28. --------
  29. one
  30. (1 row)
  31. select concat_ws('#','one');
  32. concat_ws
  33. -----------
  34. one
  35. (1 row)
  36. select concat_ws(',',10,20,null,30);
  37. concat_ws
  38. -----------
  39. 10,20,30
  40. (1 row)
  41. select concat_ws('',10,20,null,30);
  42. concat_ws
  43. -----------
  44. 102030
  45. (1 row)
  46. select concat_ws(NULL,10,20,null,30) is null;
  47. ?column?
  48. ----------
  49. t
  50. (1 row)
  51. select reverse('abcde');
  52. reverse
  53. ---------
  54. edcba
  55. (1 row)
  56. select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i;
  57. i | left | right
  58. ----+------+-------
  59. -5 | |
  60. -4 | |
  61. -3 | a | j
  62. -2 | ah | oj
  63. -1 | aho | hoj
  64. 0 | |
  65. 1 | a | j
  66. 2 | ah | oj
  67. 3 | aho | hoj
  68. 4 | ahoj | ahoj
  69. 5 | ahoj | ahoj
  70. (11 rows)
  71. select quote_literal('');
  72. quote_literal
  73. ---------------
  74. ''
  75. (1 row)
  76. select quote_literal('abc''');
  77. quote_literal
  78. ---------------
  79. 'abc'''
  80. (1 row)
  81. select quote_literal(e'\\');
  82. quote_literal
  83. ---------------
  84. E'\\'
  85. (1 row)
  86. /*
  87. * format
  88. */
  89. select format(NULL);
  90. format
  91. --------
  92. (1 row)
  93. select format('Hello');
  94. format
  95. --------
  96. Hello
  97. (1 row)
  98. select format('Hello %s', 'World');
  99. format
  100. -------------
  101. Hello World
  102. (1 row)
  103. select format('Hello %%');
  104. format
  105. ---------
  106. Hello %
  107. (1 row)
  108. select format('Hello %%%%');
  109. format
  110. ----------
  111. Hello %%
  112. (1 row)
  113. -- should fail
  114. select format('Hello %s %s', 'World');
  115. ERROR: too few arguments for format()
  116. select format('Hello %s');
  117. ERROR: too few arguments for format()
  118. select format('Hello %x', 20);
  119. ERROR: unrecognized format() type specifier "x"
  120. HINT: For a single "%" use "%%".
  121. -- check literal and sql identifiers
  122. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello');
  123. format
  124. ----------------------------------------
  125. INSERT INTO mytab VALUES('10','Hello')
  126. (1 row)
  127. select format('%s%s%s','Hello', NULL,'World');
  128. format
  129. ------------
  130. HelloWorld
  131. (1 row)
  132. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, NULL);
  133. format
  134. -------------------------------------
  135. INSERT INTO mytab VALUES('10',NULL)
  136. (1 row)
  137. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', NULL, 'Hello');
  138. format
  139. ----------------------------------------
  140. INSERT INTO mytab VALUES(NULL,'Hello')
  141. (1 row)
  142. -- should fail, sql identifier cannot be NULL
  143. select format('INSERT INTO %I VALUES(%L,%L)', NULL, 10, 'Hello');
  144. ERROR: null values cannot be formatted as an SQL identifier
  145. -- check positional placeholders
  146. select format('%1$s %3$s', 1, 2, 3);
  147. format
  148. --------
  149. 1 3
  150. (1 row)
  151. select format('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  152. format
  153. --------
  154. 1 12
  155. (1 row)
  156. -- should fail
  157. select format('%1$s %4$s', 1, 2, 3);
  158. ERROR: too few arguments for format()
  159. select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  160. ERROR: too few arguments for format()
  161. select format('%0$s', 'Hello');
  162. ERROR: format specifies argument 0, but arguments are numbered from 1
  163. select format('%*0$s', 'Hello');
  164. ERROR: format specifies argument 0, but arguments are numbered from 1
  165. select format('%1$', 1);
  166. ERROR: unterminated format() type specifier
  167. HINT: For a single "%" use "%%".
  168. select format('%1$1', 1);
  169. ERROR: unterminated format() type specifier
  170. HINT: For a single "%" use "%%".
  171. -- check mix of positional and ordered placeholders
  172. select format('Hello %s %1$s %s', 'World', 'Hello again');
  173. format
  174. -------------------------------
  175. Hello World World Hello again
  176. (1 row)
  177. select format('Hello %s %s, %2$s %2$s', 'World', 'Hello again');
  178. format
  179. --------------------------------------------------
  180. Hello World Hello again, Hello again Hello again
  181. (1 row)
  182. -- check field widths and left, right alignment
  183. select format('>>%10s<<', 'Hello');
  184. format
  185. ----------------
  186. >> Hello<<
  187. (1 row)
  188. select format('>>%10s<<', NULL);
  189. format
  190. ----------------
  191. >> <<
  192. (1 row)
  193. select format('>>%10s<<', '');
  194. format
  195. ----------------
  196. >> <<
  197. (1 row)
  198. select format('>>%-10s<<', '');
  199. format
  200. ----------------
  201. >> <<
  202. (1 row)
  203. select format('>>%-10s<<', 'Hello');
  204. format
  205. ----------------
  206. >>Hello <<
  207. (1 row)
  208. select format('>>%-10s<<', NULL);
  209. format
  210. ----------------
  211. >> <<
  212. (1 row)
  213. select format('>>%1$10s<<', 'Hello');
  214. format
  215. ----------------
  216. >> Hello<<
  217. (1 row)
  218. select format('>>%1$-10I<<', 'Hello');
  219. format
  220. ----------------
  221. >>"Hello" <<
  222. (1 row)
  223. select format('>>%2$*1$L<<', 10, 'Hello');
  224. format
  225. ----------------
  226. >> 'Hello'<<
  227. (1 row)
  228. select format('>>%2$*1$L<<', 10, NULL);
  229. format
  230. ----------------
  231. >> NULL<<
  232. (1 row)
  233. select format('>>%2$*1$L<<', -10, NULL);
  234. format
  235. ----------------
  236. >>NULL <<
  237. (1 row)
  238. select format('>>%*s<<', 10, 'Hello');
  239. format
  240. ----------------
  241. >> Hello<<
  242. (1 row)
  243. select format('>>%*1$s<<', 10, 'Hello');
  244. format
  245. ----------------
  246. >> Hello<<
  247. (1 row)
  248. select format('>>%-s<<', 'Hello');
  249. format
  250. -----------
  251. >>Hello<<
  252. (1 row)
  253. select format('>>%10L<<', NULL);
  254. format
  255. ----------------
  256. >> NULL<<
  257. (1 row)
  258. select format('>>%2$*1$L<<', NULL, 'Hello');
  259. format
  260. -------------
  261. >>'Hello'<<
  262. (1 row)
  263. select format('>>%2$*1$L<<', 0, 'Hello');
  264. format
  265. -------------
  266. >>'Hello'<<
  267. (1 row)