text.out 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. -- As of 8.3 we have removed most implicit casts to text, so that for example
  24. -- this no longer works:
  25. select length(42);
  26. ERROR: function length(integer) does not exist
  27. LINE 1: select length(42);
  28. ^
  29. HINT: No function matches the given name and argument types. You might need to add explicit type casts.
  30. -- But as a special exception for usability's sake, we still allow implicit
  31. -- casting to text in concatenations, so long as the other input is text or
  32. -- an unknown literal. So these work:
  33. select 'four: '::text || 2+2;
  34. ?column?
  35. ----------
  36. four: 4
  37. (1 row)
  38. select 'four: ' || 2+2;
  39. ?column?
  40. ----------
  41. four: 4
  42. (1 row)
  43. -- but not this:
  44. select 3 || 4.0;
  45. ERROR: operator does not exist: integer || numeric
  46. LINE 1: select 3 || 4.0;
  47. ^
  48. HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
  49. /*
  50. * various string functions
  51. */
  52. select concat('one');
  53. concat
  54. --------
  55. one
  56. (1 row)
  57. select concat(1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
  58. concat
  59. ----------------------
  60. 123hellotf03-09-2010
  61. (1 row)
  62. select concat_ws('#','one');
  63. concat_ws
  64. -----------
  65. one
  66. (1 row)
  67. select concat_ws('#',1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
  68. concat_ws
  69. ----------------------------
  70. 1#2#3#hello#t#f#03-09-2010
  71. (1 row)
  72. select concat_ws(',',10,20,null,30);
  73. concat_ws
  74. -----------
  75. 10,20,30
  76. (1 row)
  77. select concat_ws('',10,20,null,30);
  78. concat_ws
  79. -----------
  80. 102030
  81. (1 row)
  82. select concat_ws(NULL,10,20,null,30) is null;
  83. ?column?
  84. ----------
  85. t
  86. (1 row)
  87. select reverse('abcde');
  88. reverse
  89. ---------
  90. edcba
  91. (1 row)
  92. select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i;
  93. i | left | right
  94. ----+------+-------
  95. -5 | |
  96. -4 | |
  97. -3 | a | j
  98. -2 | ah | oj
  99. -1 | aho | hoj
  100. 0 | |
  101. 1 | a | j
  102. 2 | ah | oj
  103. 3 | aho | hoj
  104. 4 | ahoj | ahoj
  105. 5 | ahoj | ahoj
  106. (11 rows)
  107. select quote_literal('');
  108. quote_literal
  109. ---------------
  110. ''
  111. (1 row)
  112. select quote_literal('abc''');
  113. quote_literal
  114. ---------------
  115. 'abc'''
  116. (1 row)
  117. select quote_literal(e'\\');
  118. quote_literal
  119. ---------------
  120. E'\\'
  121. (1 row)
  122. -- check variadic labeled argument
  123. select concat(variadic array[1,2,3]);
  124. concat
  125. --------
  126. 123
  127. (1 row)
  128. select concat_ws(',', variadic array[1,2,3]);
  129. concat_ws
  130. -----------
  131. 1,2,3
  132. (1 row)
  133. select concat_ws(',', variadic NULL::int[]);
  134. concat_ws
  135. -----------
  136. (1 row)
  137. select concat(variadic NULL::int[]) is NULL;
  138. ?column?
  139. ----------
  140. t
  141. (1 row)
  142. select concat(variadic '{}'::int[]) = '';
  143. ?column?
  144. ----------
  145. t
  146. (1 row)
  147. --should fail
  148. select concat_ws(',', variadic 10);
  149. ERROR: VARIADIC argument must be an array
  150. LINE 1: select concat_ws(',', variadic 10);
  151. ^
  152. /*
  153. * format
  154. */
  155. select format(NULL);
  156. format
  157. --------
  158. (1 row)
  159. select format('Hello');
  160. format
  161. --------
  162. Hello
  163. (1 row)
  164. select format('Hello %s', 'World');
  165. format
  166. -------------
  167. Hello World
  168. (1 row)
  169. select format('Hello %%');
  170. format
  171. ---------
  172. Hello %
  173. (1 row)
  174. select format('Hello %%%%');
  175. format
  176. ----------
  177. Hello %%
  178. (1 row)
  179. -- should fail
  180. select format('Hello %s %s', 'World');
  181. ERROR: too few arguments for format()
  182. select format('Hello %s');
  183. ERROR: too few arguments for format()
  184. select format('Hello %x', 20);
  185. ERROR: unrecognized format() type specifier "x"
  186. HINT: For a single "%" use "%%".
  187. -- check literal and sql identifiers
  188. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello');
  189. format
  190. ----------------------------------------
  191. INSERT INTO mytab VALUES('10','Hello')
  192. (1 row)
  193. select format('%s%s%s','Hello', NULL,'World');
  194. format
  195. ------------
  196. HelloWorld
  197. (1 row)
  198. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, NULL);
  199. format
  200. -------------------------------------
  201. INSERT INTO mytab VALUES('10',NULL)
  202. (1 row)
  203. select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', NULL, 'Hello');
  204. format
  205. ----------------------------------------
  206. INSERT INTO mytab VALUES(NULL,'Hello')
  207. (1 row)
  208. -- should fail, sql identifier cannot be NULL
  209. select format('INSERT INTO %I VALUES(%L,%L)', NULL, 10, 'Hello');
  210. ERROR: null values cannot be formatted as an SQL identifier
  211. -- check positional placeholders
  212. select format('%1$s %3$s', 1, 2, 3);
  213. format
  214. --------
  215. 1 3
  216. (1 row)
  217. select format('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  218. format
  219. --------
  220. 1 12
  221. (1 row)
  222. -- should fail
  223. select format('%1$s %4$s', 1, 2, 3);
  224. ERROR: too few arguments for format()
  225. select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  226. ERROR: too few arguments for format()
  227. select format('%0$s', 'Hello');
  228. ERROR: format specifies argument 0, but arguments are numbered from 1
  229. select format('%*0$s', 'Hello');
  230. ERROR: format specifies argument 0, but arguments are numbered from 1
  231. select format('%1$', 1);
  232. ERROR: unterminated format() type specifier
  233. HINT: For a single "%" use "%%".
  234. select format('%1$1', 1);
  235. ERROR: unterminated format() type specifier
  236. HINT: For a single "%" use "%%".
  237. -- check mix of positional and ordered placeholders
  238. select format('Hello %s %1$s %s', 'World', 'Hello again');
  239. format
  240. -------------------------------
  241. Hello World World Hello again
  242. (1 row)
  243. select format('Hello %s %s, %2$s %2$s', 'World', 'Hello again');
  244. format
  245. --------------------------------------------------
  246. Hello World Hello again, Hello again Hello again
  247. (1 row)
  248. -- check variadic labeled arguments
  249. select format('%s, %s', variadic array['Hello','World']);
  250. format
  251. --------------
  252. Hello, World
  253. (1 row)
  254. select format('%s, %s', variadic array[1, 2]);
  255. format
  256. --------
  257. 1, 2
  258. (1 row)
  259. select format('%s, %s', variadic array[true, false]);
  260. format
  261. --------
  262. t, f
  263. (1 row)
  264. select format('%s, %s', variadic array[true, false]::text[]);
  265. format
  266. -------------
  267. true, false
  268. (1 row)
  269. -- check variadic with positional placeholders
  270. select format('%2$s, %1$s', variadic array['first', 'second']);
  271. format
  272. ---------------
  273. second, first
  274. (1 row)
  275. select format('%2$s, %1$s', variadic array[1, 2]);
  276. format
  277. --------
  278. 2, 1
  279. (1 row)
  280. -- variadic argument can be array type NULL, but should not be referenced
  281. select format('Hello', variadic NULL::int[]);
  282. format
  283. --------
  284. Hello
  285. (1 row)
  286. -- variadic argument allows simulating more than FUNC_MAX_ARGS parameters
  287. select format(string_agg('%s',','), variadic array_agg(i))
  288. from generate_series(1,200) g(i);
  289. format
  290. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  291. 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,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,115,116,117,118,119,120,121,122,123,124,125,126,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,192,193,194,195,196,197,198,199,200
  292. (1 row)
  293. -- check field widths and left, right alignment
  294. select format('>>%10s<<', 'Hello');
  295. format
  296. ----------------
  297. >> Hello<<
  298. (1 row)
  299. select format('>>%10s<<', NULL);
  300. format
  301. ----------------
  302. >> <<
  303. (1 row)
  304. select format('>>%10s<<', '');
  305. format
  306. ----------------
  307. >> <<
  308. (1 row)
  309. select format('>>%-10s<<', '');
  310. format
  311. ----------------
  312. >> <<
  313. (1 row)
  314. select format('>>%-10s<<', 'Hello');
  315. format
  316. ----------------
  317. >>Hello <<
  318. (1 row)
  319. select format('>>%-10s<<', NULL);
  320. format
  321. ----------------
  322. >> <<
  323. (1 row)
  324. select format('>>%1$10s<<', 'Hello');
  325. format
  326. ----------------
  327. >> Hello<<
  328. (1 row)
  329. select format('>>%1$-10I<<', 'Hello');
  330. format
  331. ----------------
  332. >>"Hello" <<
  333. (1 row)
  334. select format('>>%2$*1$L<<', 10, 'Hello');
  335. format
  336. ----------------
  337. >> 'Hello'<<
  338. (1 row)
  339. select format('>>%2$*1$L<<', 10, NULL);
  340. format
  341. ----------------
  342. >> NULL<<
  343. (1 row)
  344. select format('>>%2$*1$L<<', -10, NULL);
  345. format
  346. ----------------
  347. >>NULL <<
  348. (1 row)
  349. select format('>>%*s<<', 10, 'Hello');
  350. format
  351. ----------------
  352. >> Hello<<
  353. (1 row)
  354. select format('>>%*1$s<<', 10, 'Hello');
  355. format
  356. ----------------
  357. >> Hello<<
  358. (1 row)
  359. select format('>>%-s<<', 'Hello');
  360. format
  361. -----------
  362. >>Hello<<
  363. (1 row)
  364. select format('>>%10L<<', NULL);
  365. format
  366. ----------------
  367. >> NULL<<
  368. (1 row)
  369. select format('>>%2$*1$L<<', NULL, 'Hello');
  370. format
  371. -------------
  372. >>'Hello'<<
  373. (1 row)
  374. select format('>>%2$*1$L<<', 0, 'Hello');
  375. format
  376. -------------
  377. >>'Hello'<<
  378. (1 row)