strings.out 61 KB

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