1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272 |
- --
- -- STRINGS
- -- Test various data entry syntaxes.
- --
- -- SQL string continuation syntax
- -- E021-03 character string literals
- SELECT 'first line'
- ' - next line'
- ' - third line'
- AS "Three lines to one";
- Three lines to one
- -------------------------------------
- first line - next line - third line
- (1 row)
- -- illegal string continuation syntax
- SELECT 'first line'
- ' - next line' /* this comment is not allowed here */
- ' - third line'
- AS "Illegal comment within continuation";
- ERROR: syntax error at or near "' - third line'"
- LINE 3: ' - third line'
- ^
- -- Unicode escapes
- SET standard_conforming_strings TO on;
- SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
- data
- ------
- data
- (1 row)
- SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
- dat\+000061
- -------------
- dat\+000061
- (1 row)
- SELECT U&'a\\b' AS "a\b";
- a\b
- -----
- a\b
- (1 row)
- SELECT U&' \' UESCAPE '!' AS "tricky";
- tricky
- --------
- \
- (1 row)
- SELECT 'tricky' AS U&"\" UESCAPE '!';
- \
- --------
- tricky
- (1 row)
- SELECT U&'wrong: \061';
- ERROR: invalid Unicode escape
- LINE 1: SELECT U&'wrong: \061';
- ^
- HINT: Unicode escapes must be \XXXX or \+XXXXXX.
- SELECT U&'wrong: \+0061';
- ERROR: invalid Unicode escape
- LINE 1: SELECT U&'wrong: \+0061';
- ^
- HINT: Unicode escapes must be \XXXX or \+XXXXXX.
- SELECT U&'wrong: +0061' UESCAPE +;
- ERROR: UESCAPE must be followed by a simple string literal at or near "+"
- LINE 1: SELECT U&'wrong: +0061' UESCAPE +;
- ^
- SELECT U&'wrong: +0061' UESCAPE '+';
- ERROR: invalid Unicode escape character at or near "'+'"
- LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
- ^
- SELECT U&'wrong: \db99';
- ERROR: invalid Unicode surrogate pair
- LINE 1: SELECT U&'wrong: \db99';
- ^
- SELECT U&'wrong: \db99xy';
- ERROR: invalid Unicode surrogate pair
- LINE 1: SELECT U&'wrong: \db99xy';
- ^
- SELECT U&'wrong: \db99\\';
- ERROR: invalid Unicode surrogate pair
- LINE 1: SELECT U&'wrong: \db99\\';
- ^
- SELECT U&'wrong: \db99\0061';
- ERROR: invalid Unicode surrogate pair
- LINE 1: SELECT U&'wrong: \db99\0061';
- ^
- SELECT U&'wrong: \+00db99\+000061';
- ERROR: invalid Unicode surrogate pair
- LINE 1: SELECT U&'wrong: \+00db99\+000061';
- ^
- SELECT U&'wrong: \+2FFFFF';
- ERROR: invalid Unicode escape value
- LINE 1: SELECT U&'wrong: \+2FFFFF';
- ^
- -- while we're here, check the same cases in E-style literals
- SELECT E'd\u0061t\U00000061' AS "data";
- data
- ------
- data
- (1 row)
- SELECT E'a\\b' AS "a\b";
- a\b
- -----
- a\b
- (1 row)
- SELECT E'wrong: \u061';
- ERROR: invalid Unicode escape
- LINE 1: SELECT E'wrong: \u061';
- ^
- HINT: Unicode escapes must be \uXXXX or \UXXXXXXXX.
- SELECT E'wrong: \U0061';
- ERROR: invalid Unicode escape
- LINE 1: SELECT E'wrong: \U0061';
- ^
- HINT: Unicode escapes must be \uXXXX or \UXXXXXXXX.
- SELECT E'wrong: \udb99';
- ERROR: invalid Unicode surrogate pair at or near "'"
- LINE 1: SELECT E'wrong: \udb99';
- ^
- SELECT E'wrong: \udb99xy';
- ERROR: invalid Unicode surrogate pair at or near "x"
- LINE 1: SELECT E'wrong: \udb99xy';
- ^
- SELECT E'wrong: \udb99\\';
- ERROR: invalid Unicode surrogate pair at or near "\"
- LINE 1: SELECT E'wrong: \udb99\\';
- ^
- SELECT E'wrong: \udb99\u0061';
- ERROR: invalid Unicode surrogate pair at or near "\u0061"
- LINE 1: SELECT E'wrong: \udb99\u0061';
- ^
- SELECT E'wrong: \U0000db99\U00000061';
- ERROR: invalid Unicode surrogate pair at or near "\U00000061"
- LINE 1: SELECT E'wrong: \U0000db99\U00000061';
- ^
- SELECT E'wrong: \U002FFFFF';
- ERROR: invalid Unicode escape value at or near "\U002FFFFF"
- LINE 1: SELECT E'wrong: \U002FFFFF';
- ^
- SET standard_conforming_strings TO off;
- SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
- ERROR: unsafe use of string constant with Unicode escapes
- LINE 1: SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
- ^
- DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
- SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
- ERROR: unsafe use of string constant with Unicode escapes
- LINE 1: SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061...
- ^
- DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
- SELECT U&' \' UESCAPE '!' AS "tricky";
- ERROR: unsafe use of string constant with Unicode escapes
- LINE 1: SELECT U&' \' UESCAPE '!' AS "tricky";
- ^
- DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
- SELECT 'tricky' AS U&"\" UESCAPE '!';
- \
- --------
- tricky
- (1 row)
- SELECT U&'wrong: \061';
- ERROR: unsafe use of string constant with Unicode escapes
- LINE 1: SELECT U&'wrong: \061';
- ^
- DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
- SELECT U&'wrong: \+0061';
- ERROR: unsafe use of string constant with Unicode escapes
- LINE 1: SELECT U&'wrong: \+0061';
- ^
- DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
- SELECT U&'wrong: +0061' UESCAPE '+';
- ERROR: unsafe use of string constant with Unicode escapes
- LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
- ^
- DETAIL: String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
- RESET standard_conforming_strings;
- -- bytea
- SET bytea_output TO hex;
- SELECT E'\\xDeAdBeEf'::bytea;
- bytea
- ------------
- \xdeadbeef
- (1 row)
- SELECT E'\\x De Ad Be Ef '::bytea;
- bytea
- ------------
- \xdeadbeef
- (1 row)
- SELECT E'\\xDeAdBeE'::bytea;
- ERROR: invalid hexadecimal data: odd number of digits
- LINE 1: SELECT E'\\xDeAdBeE'::bytea;
- ^
- SELECT E'\\xDeAdBeEx'::bytea;
- ERROR: invalid hexadecimal digit: "x"
- LINE 1: SELECT E'\\xDeAdBeEx'::bytea;
- ^
- SELECT E'\\xDe00BeEf'::bytea;
- bytea
- ------------
- \xde00beef
- (1 row)
- SELECT E'DeAdBeEf'::bytea;
- bytea
- --------------------
- \x4465416442654566
- (1 row)
- SELECT E'De\\000dBeEf'::bytea;
- bytea
- --------------------
- \x4465006442654566
- (1 row)
- SELECT E'De\123dBeEf'::bytea;
- bytea
- --------------------
- \x4465536442654566
- (1 row)
- SELECT E'De\\123dBeEf'::bytea;
- bytea
- --------------------
- \x4465536442654566
- (1 row)
- SELECT E'De\\678dBeEf'::bytea;
- ERROR: invalid input syntax for type bytea
- LINE 1: SELECT E'De\\678dBeEf'::bytea;
- ^
- SET bytea_output TO escape;
- SELECT E'\\xDeAdBeEf'::bytea;
- bytea
- ------------------
- \336\255\276\357
- (1 row)
- SELECT E'\\x De Ad Be Ef '::bytea;
- bytea
- ------------------
- \336\255\276\357
- (1 row)
- SELECT E'\\xDe00BeEf'::bytea;
- bytea
- ------------------
- \336\000\276\357
- (1 row)
- SELECT E'DeAdBeEf'::bytea;
- bytea
- ----------
- DeAdBeEf
- (1 row)
- SELECT E'De\\000dBeEf'::bytea;
- bytea
- -------------
- De\000dBeEf
- (1 row)
- SELECT E'De\\123dBeEf'::bytea;
- bytea
- ----------
- DeSdBeEf
- (1 row)
- --
- -- test conversions between various string types
- -- E021-10 implicit casting among the character data types
- --
- SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
- text(char)
- ------------
- a
- ab
- abcd
- abcd
- (4 rows)
- SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
- text(varchar)
- ---------------
- a
- ab
- abcd
- abcd
- (4 rows)
- SELECT CAST(name 'namefield' AS text) AS "text(name)";
- text(name)
- ------------
- namefield
- (1 row)
- -- since this is an explicit cast, it should truncate w/o error:
- SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
- char(text)
- ------------
- doh!
- hi de ho n
- (2 rows)
- -- note: implicit-cast case is tested in char.sql
- SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
- char(text)
- ----------------------
- doh!
- hi de ho neighbor
- (2 rows)
- SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
- char(varchar)
- ---------------
- a
- ab
- abcd
- abcd
- (4 rows)
- SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
- char(name)
- ------------
- namefield
- (1 row)
- SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
- varchar(text)
- -------------------
- doh!
- hi de ho neighbor
- (2 rows)
- SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
- varchar(char)
- ---------------
- a
- ab
- abcd
- abcd
- (4 rows)
- SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
- varchar(name)
- ---------------
- namefield
- (1 row)
- --
- -- test SQL string functions
- -- E### and T### are feature reference numbers from SQL99
- --
- -- E021-09 trim function
- SELECT TRIM(BOTH FROM ' bunch o blanks ') = 'bunch o blanks' AS "bunch o blanks";
- bunch o blanks
- ----------------
- t
- (1 row)
- SELECT TRIM(LEADING FROM ' bunch o blanks ') = 'bunch o blanks ' AS "bunch o blanks ";
- bunch o blanks
- ------------------
- t
- (1 row)
- SELECT TRIM(TRAILING FROM ' bunch o blanks ') = ' bunch o blanks' AS " bunch o blanks";
- bunch o blanks
- ------------------
- t
- (1 row)
- SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
- some Xs
- ---------
- t
- (1 row)
- -- E021-06 substring expression
- SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
- 34567890
- ----------
- t
- (1 row)
- SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
- 456
- -----
- t
- (1 row)
- -- test overflow cases
- SELECT SUBSTRING('string' FROM 2 FOR 2147483646) AS "tring";
- tring
- -------
- tring
- (1 row)
- SELECT SUBSTRING('string' FROM -10 FOR 2147483646) AS "string";
- string
- --------
- string
- (1 row)
- SELECT SUBSTRING('string' FROM -10 FOR -2147483646) AS "error";
- ERROR: negative substring length not allowed
- -- T581 regular expression substring (with SQL's bizarre regexp syntax)
- SELECT SUBSTRING('abcdefg' SIMILAR 'a#"(b_d)#"%' ESCAPE '#') AS "bcd";
- bcd
- -----
- bcd
- (1 row)
- -- obsolete SQL99 syntax
- SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
- bcd
- -----
- bcd
- (1 row)
- -- No match should return NULL
- SELECT SUBSTRING('abcdefg' SIMILAR '#"(b_d)#"%' ESCAPE '#') IS NULL AS "True";
- True
- ------
- t
- (1 row)
- -- Null inputs should return NULL
- SELECT SUBSTRING('abcdefg' SIMILAR '%' ESCAPE NULL) IS NULL AS "True";
- True
- ------
- t
- (1 row)
- SELECT SUBSTRING(NULL SIMILAR '%' ESCAPE '#') IS NULL AS "True";
- True
- ------
- t
- (1 row)
- SELECT SUBSTRING('abcdefg' SIMILAR NULL ESCAPE '#') IS NULL AS "True";
- True
- ------
- t
- (1 row)
- -- The first and last parts should act non-greedy
- SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"g' ESCAPE '#') AS "bcdef";
- bcdef
- -------
- bcdef
- (1 row)
- SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*' ESCAPE '#') AS "abcdefg";
- abcdefg
- ---------
- abcdefg
- (1 row)
- -- Vertical bar in any part affects only that part
- SELECT SUBSTRING('abcdefg' SIMILAR 'a|b#"%#"g' ESCAPE '#') AS "bcdef";
- bcdef
- -------
- bcdef
- (1 row)
- SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"x|g' ESCAPE '#') AS "bcdef";
- bcdef
- -------
- bcdef
- (1 row)
- SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%|ab#"g' ESCAPE '#') AS "bcdef";
- bcdef
- -------
- bcdef
- (1 row)
- -- Can't have more than two part separators
- SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*#"x' ESCAPE '#') AS "error";
- ERROR: SQL regular expression may not contain more than two escape-double-quote separators
- CONTEXT: SQL function "substring" statement 1
- -- Postgres extension: with 0 or 1 separator, assume parts 1 and 3 are empty
- SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%g' ESCAPE '#') AS "bcdefg";
- bcdefg
- --------
- bcdefg
- (1 row)
- SELECT SUBSTRING('abcdefg' SIMILAR 'a%g' ESCAPE '#') AS "abcdefg";
- abcdefg
- ---------
- abcdefg
- (1 row)
- -- substring() with just two arguments is not allowed by SQL spec;
- -- we accept it, but we interpret the pattern as a POSIX regexp not SQL
- SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
- cde
- -----
- cde
- (1 row)
- -- With a parenthesized subexpression, return only what matches the subexpr
- SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
- cde
- -----
- cde
- (1 row)
- -- Check behavior of SIMILAR TO, which uses largely the same regexp variant
- SELECT 'abcdefg' SIMILAR TO '_bcd%' AS true;
- true
- ------
- t
- (1 row)
- SELECT 'abcdefg' SIMILAR TO 'bcd%' AS false;
- false
- -------
- f
- (1 row)
- SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '#' AS false;
- false
- -------
- f
- (1 row)
- SELECT 'abcd%' SIMILAR TO '_bcd#%' ESCAPE '#' AS true;
- true
- ------
- t
- (1 row)
- -- Postgres uses '\' as the default escape character, which is not per spec
- SELECT 'abcdefg' SIMILAR TO '_bcd\%' AS false;
- false
- -------
- f
- (1 row)
- -- and an empty string to mean "no escape", which is also not per spec
- SELECT 'abcd\efg' SIMILAR TO '_bcd\%' ESCAPE '' AS true;
- true
- ------
- t
- (1 row)
- -- these behaviors are per spec, though:
- SELECT 'abcdefg' SIMILAR TO '_bcd%' ESCAPE NULL AS null;
- null
- ------
-
- (1 row)
- SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '##' AS error;
- ERROR: invalid escape string
- HINT: Escape string must be empty or one character.
- -- Test back reference in regexp_replace
- SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
- regexp_replace
- ----------------
- (111) 222-3333
- (1 row)
- SELECT regexp_replace('AAA BBB CCC ', E'\\s+', ' ', 'g');
- regexp_replace
- ----------------
- AAA BBB CCC
- (1 row)
- SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
- regexp_replace
- ----------------
- ZAAAZ
- (1 row)
- SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
- regexp_replace
- ----------------
- Z Z
- (1 row)
- -- invalid regexp option
- SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
- ERROR: invalid regular expression option: "z"
- -- set so we can tell NULL from empty string
- \pset null '\\N'
- -- return all matches from regexp
- SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
- regexp_matches
- ----------------
- {bar,beque}
- (1 row)
- -- test case insensitive
- SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
- regexp_matches
- ----------------
- {bAR,bEqUE}
- (1 row)
- -- global option - more than one match
- SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
- regexp_matches
- ----------------
- {bar,beque}
- {bazil,barf}
- (2 rows)
- -- empty capture group (matched empty string)
- SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
- regexp_matches
- ----------------
- {bar,"",beque}
- (1 row)
- -- no match
- SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
- regexp_matches
- ----------------
- (0 rows)
- -- optional capture group did not match, null entry in array
- SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
- regexp_matches
- ------------------
- {bar,NULL,beque}
- (1 row)
- -- no capture groups
- SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
- regexp_matches
- ----------------
- {barbeque}
- (1 row)
- -- start/end-of-line matches are of zero length
- SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '^', 'mg');
- regexp_matches
- ----------------
- {""}
- {""}
- {""}
- {""}
- (4 rows)
- SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '$', 'mg');
- regexp_matches
- ----------------
- {""}
- {""}
- {""}
- {""}
- (4 rows)
- SELECT regexp_matches('1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '^.?', 'mg');
- regexp_matches
- ----------------
- {1}
- {2}
- {3}
- {4}
- {""}
- (5 rows)
- SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '.?$', 'mg');
- regexp_matches
- ----------------
- {""}
- {1}
- {""}
- {2}
- {""}
- {3}
- {""}
- {4}
- {""}
- {""}
- (10 rows)
- SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4', '.?$', 'mg');
- regexp_matches
- ----------------
- {""}
- {1}
- {""}
- {2}
- {""}
- {3}
- {""}
- {4}
- {""}
- (9 rows)
- -- give me errors
- SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
- ERROR: invalid regular expression option: "z"
- SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
- ERROR: invalid regular expression: parentheses () not balanced
- SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
- ERROR: invalid regular expression: invalid repetition count(s)
- -- split string on regexp
- SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo;
- foo | length
- -------+--------
- the | 3
- quick | 5
- brown | 5
- fox | 3
- jumps | 5
- over | 4
- the | 3
- lazy | 4
- dog | 3
- (9 rows)
- SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$);
- regexp_split_to_array
- -----------------------------------------------
- {the,quick,brown,fox,jumps,over,the,lazy,dog}
- (1 row)
- SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo;
- foo | length
- -----+--------
- t | 1
- h | 1
- e | 1
- q | 1
- u | 1
- i | 1
- c | 1
- k | 1
- b | 1
- r | 1
- o | 1
- w | 1
- n | 1
- f | 1
- o | 1
- x | 1
- j | 1
- u | 1
- m | 1
- p | 1
- s | 1
- o | 1
- v | 1
- e | 1
- r | 1
- t | 1
- h | 1
- e | 1
- l | 1
- a | 1
- z | 1
- y | 1
- d | 1
- o | 1
- g | 1
- (35 rows)
- SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$);
- regexp_split_to_array
- -------------------------------------------------------------------------
- {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}
- (1 row)
- SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo;
- foo | length
- -----+--------
- t | 1
- h | 1
- e | 1
- | 1
- q | 1
- u | 1
- i | 1
- c | 1
- k | 1
- | 1
- b | 1
- r | 1
- o | 1
- w | 1
- n | 1
- | 1
- f | 1
- o | 1
- x | 1
- | 1
- j | 1
- u | 1
- m | 1
- p | 1
- s | 1
- | 1
- o | 1
- v | 1
- e | 1
- r | 1
- | 1
- t | 1
- h | 1
- e | 1
- | 1
- l | 1
- a | 1
- z | 1
- y | 1
- | 1
- d | 1
- o | 1
- g | 1
- (43 rows)
- SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '');
- regexp_split_to_array
- ---------------------------------------------------------------------------------------------------------
- {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}
- (1 row)
- -- case insensitive
- SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo;
- foo | length
- ---------------------------+--------
- th | 2
- QUick bROWn FOx jUMPs ov | 25
- r Th | 4
- lazy dOG | 9
- (4 rows)
- SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i');
- regexp_split_to_array
- -----------------------------------------------------
- {th," QUick bROWn FOx jUMPs ov","r Th"," lazy dOG"}
- (1 row)
- -- no match of pattern
- SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo;
- foo | length
- ---------------------------------------------+--------
- the quick brown fox jumps over the lazy dog | 43
- (1 row)
- SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch');
- regexp_split_to_array
- -------------------------------------------------
- {"the quick brown fox jumps over the lazy dog"}
- (1 row)
- -- some corner cases
- SELECT regexp_split_to_array('123456','1');
- regexp_split_to_array
- -----------------------
- {"",23456}
- (1 row)
- SELECT regexp_split_to_array('123456','6');
- regexp_split_to_array
- -----------------------
- {12345,""}
- (1 row)
- SELECT regexp_split_to_array('123456','.');
- regexp_split_to_array
- ------------------------
- {"","","","","","",""}
- (1 row)
- SELECT regexp_split_to_array('123456','');
- regexp_split_to_array
- -----------------------
- {1,2,3,4,5,6}
- (1 row)
- SELECT regexp_split_to_array('123456','(?:)');
- regexp_split_to_array
- -----------------------
- {1,2,3,4,5,6}
- (1 row)
- SELECT regexp_split_to_array('1','');
- regexp_split_to_array
- -----------------------
- {1}
- (1 row)
- -- errors
- SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo;
- ERROR: invalid regular expression option: "z"
- SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz');
- ERROR: invalid regular expression option: "z"
- -- global option meaningless for regexp_split
- SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo;
- ERROR: regexp_split_to_table() does not support the "global" option
- SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g');
- ERROR: regexp_split_to_array() does not support the "global" option
- -- change NULL-display back
- \pset null ''
- -- E021-11 position expression
- SELECT POSITION('4' IN '1234567890') = '4' AS "4";
- 4
- ---
- t
- (1 row)
- SELECT POSITION('5' IN '1234567890') = '5' AS "5";
- 5
- ---
- t
- (1 row)
- -- T312 character overlay function
- SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
- abc45f
- --------
- abc45f
- (1 row)
- SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
- yabadaba
- ----------
- yabadaba
- (1 row)
- SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
- yabadabadoo
- -------------
- yabadabadoo
- (1 row)
- SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
- bubba
- -------
- bubba
- (1 row)
- --
- -- test LIKE
- -- Be sure to form every test as a LIKE/NOT LIKE pair.
- --
- -- simplest examples
- -- E061-04 like predicate
- SELECT 'hawkeye' LIKE 'h%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'hawkeye' LIKE 'H%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' LIKE 'indio%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' LIKE 'h%eye' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'indio' LIKE '_ndio' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'indio' NOT LIKE '_ndio' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'indio' LIKE 'in__o' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'indio' NOT LIKE 'in__o' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'indio' LIKE 'in_o' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'indio' NOT LIKE 'in_o' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'abc'::name LIKE '_b_' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'abc'::name NOT LIKE '_b_' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'abc'::bytea LIKE '_b_'::bytea AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'abc'::bytea NOT LIKE '_b_'::bytea AS "false";
- false
- -------
- f
- (1 row)
- -- unused escape character
- SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
- false
- -------
- f
- (1 row)
- -- escape character
- -- E061-05 like predicate with escape clause
- SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'a_c'::bytea LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'a_c'::bytea NOT LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "false";
- false
- -------
- f
- (1 row)
- -- escape character same as pattern character
- SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
- true
- ------
- t
- (1 row)
- --
- -- test ILIKE (case-insensitive LIKE)
- -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
- --
- SELECT 'hawkeye' ILIKE 'h%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'hawkeye' ILIKE 'H%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'Hawkeye' ILIKE 'h%' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
- false
- -------
- f
- (1 row)
- SELECT 'ABC'::name ILIKE '_b_' AS "true";
- true
- ------
- t
- (1 row)
- SELECT 'ABC'::name NOT ILIKE '_b_' AS "false";
- false
- -------
- f
- (1 row)
- --
- -- test %/_ combination cases, cf bugs #4821 and #5478
- --
- SELECT 'foo' LIKE '_%' as t, 'f' LIKE '_%' as t, '' LIKE '_%' as f;
- t | t | f
- ---+---+---
- t | t | f
- (1 row)
- SELECT 'foo' LIKE '%_' as t, 'f' LIKE '%_' as t, '' LIKE '%_' as f;
- t | t | f
- ---+---+---
- t | t | f
- (1 row)
- SELECT 'foo' LIKE '__%' as t, 'foo' LIKE '___%' as t, 'foo' LIKE '____%' as f;
- t | t | f
- ---+---+---
- t | t | f
- (1 row)
- SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f;
- t | t | f
- ---+---+---
- t | t | f
- (1 row)
- SELECT 'jack' LIKE '%____%' AS t;
- t
- ---
- t
- (1 row)
- --
- -- basic tests of LIKE with indexes
- --
- CREATE TABLE texttest (a text PRIMARY KEY, b int);
- SELECT * FROM texttest WHERE a LIKE '%1%';
- a | b
- ---+---
- (0 rows)
- CREATE TABLE byteatest (a bytea PRIMARY KEY, b int);
- SELECT * FROM byteatest WHERE a LIKE '%1%';
- a | b
- ---+---
- (0 rows)
- DROP TABLE texttest, byteatest;
- --
- -- test implicit type conversion
- --
- -- E021-07 character concatenation
- SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
- Concat unknown types
- ----------------------
- unknown and unknown
- (1 row)
- SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
- Concat text to unknown type
- -----------------------------
- text and unknown
- (1 row)
- SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
- Concat char to unknown type
- -----------------------------
- characters and text
- (1 row)
- SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
- Concat text to char
- ---------------------
- text and characters
- (1 row)
- SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
- Concat text to varchar
- ------------------------
- text and varchar
- (1 row)
- --
- -- test substr with toasted text values
- --
- CREATE TABLE toasttest(f1 text);
- insert into toasttest values(repeat('1234567890',10000));
- insert into toasttest values(repeat('1234567890',10000));
- --
- -- Ensure that some values are uncompressed, to test the faster substring
- -- operation used in that case
- --
- alter table toasttest alter column f1 set storage external;
- insert into toasttest values(repeat('1234567890',10000));
- insert into toasttest values(repeat('1234567890',10000));
- -- If the starting position is zero or less, then return from the start of the string
- -- adjusting the length to be consistent with the "negative start" per SQL.
- SELECT substr(f1, -1, 5) from toasttest;
- substr
- --------
- 123
- 123
- 123
- 123
- (4 rows)
- -- If the length is less than zero, an ERROR is thrown.
- SELECT substr(f1, 5, -1) from toasttest;
- ERROR: negative substring length not allowed
- -- If no third argument (length) is provided, the length to the end of the
- -- string is assumed.
- SELECT substr(f1, 99995) from toasttest;
- substr
- --------
- 567890
- 567890
- 567890
- 567890
- (4 rows)
- -- If start plus length is > string length, the result is truncated to
- -- string length
- SELECT substr(f1, 99995, 10) from toasttest;
- substr
- --------
- 567890
- 567890
- 567890
- 567890
- (4 rows)
- TRUNCATE TABLE toasttest;
- INSERT INTO toasttest values (repeat('1234567890',300));
- INSERT INTO toasttest values (repeat('1234567890',300));
- INSERT INTO toasttest values (repeat('1234567890',300));
- INSERT INTO toasttest values (repeat('1234567890',300));
- -- expect >0 blocks
- SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
- FROM pg_class where relname = 'toasttest';
- is_empty
- ----------
- f
- (1 row)
- TRUNCATE TABLE toasttest;
- ALTER TABLE toasttest set (toast_tuple_target = 4080);
- INSERT INTO toasttest values (repeat('1234567890',300));
- INSERT INTO toasttest values (repeat('1234567890',300));
- INSERT INTO toasttest values (repeat('1234567890',300));
- INSERT INTO toasttest values (repeat('1234567890',300));
- -- expect 0 blocks
- SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
- FROM pg_class where relname = 'toasttest';
- is_empty
- ----------
- t
- (1 row)
- DROP TABLE toasttest;
- --
- -- test substr with toasted bytea values
- --
- CREATE TABLE toasttest(f1 bytea);
- insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
- insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
- --
- -- Ensure that some values are uncompressed, to test the faster substring
- -- operation used in that case
- --
- alter table toasttest alter column f1 set storage external;
- insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
- insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
- -- If the starting position is zero or less, then return from the start of the string
- -- adjusting the length to be consistent with the "negative start" per SQL.
- SELECT substr(f1, -1, 5) from toasttest;
- substr
- --------
- 123
- 123
- 123
- 123
- (4 rows)
- -- If the length is less than zero, an ERROR is thrown.
- SELECT substr(f1, 5, -1) from toasttest;
- ERROR: negative substring length not allowed
- -- If no third argument (length) is provided, the length to the end of the
- -- string is assumed.
- SELECT substr(f1, 99995) from toasttest;
- substr
- --------
- 567890
- 567890
- 567890
- 567890
- (4 rows)
- -- If start plus length is > string length, the result is truncated to
- -- string length
- SELECT substr(f1, 99995, 10) from toasttest;
- substr
- --------
- 567890
- 567890
- 567890
- 567890
- (4 rows)
- DROP TABLE toasttest;
- -- test internally compressing datums
- -- this tests compressing a datum to a very small size which exercises a
- -- corner case in packed-varlena handling: even though small, the compressed
- -- datum must be given a 4-byte header because there are no bits to indicate
- -- compression in a 1-byte header
- CREATE TABLE toasttest (c char(4096));
- INSERT INTO toasttest VALUES('x');
- SELECT length(c), c::text FROM toasttest;
- length | c
- --------+---
- 1 | x
- (1 row)
- SELECT c FROM toasttest;
- c
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- x
- (1 row)
- DROP TABLE toasttest;
- --
- -- test length
- --
- SELECT length('abcdef') AS "length_6";
- length_6
- ----------
- 6
- (1 row)
- --
- -- test strpos
- --
- SELECT strpos('abcdef', 'cd') AS "pos_3";
- pos_3
- -------
- 3
- (1 row)
- SELECT strpos('abcdef', 'xy') AS "pos_0";
- pos_0
- -------
- 0
- (1 row)
- SELECT strpos('abcdef', '') AS "pos_1";
- pos_1
- -------
- 1
- (1 row)
- SELECT strpos('', 'xy') AS "pos_0";
- pos_0
- -------
- 0
- (1 row)
- SELECT strpos('', '') AS "pos_1";
- pos_1
- -------
- 1
- (1 row)
- --
- -- test replace
- --
- SELECT replace('abcdef', 'de', '45') AS "abc45f";
- abc45f
- --------
- abc45f
- (1 row)
- SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
- ya123da123doo
- ---------------
- ya123da123doo
- (1 row)
- SELECT replace('yabadoo', 'bad', '') AS "yaoo";
- yaoo
- ------
- yaoo
- (1 row)
- --
- -- test split_part
- --
- select split_part('','@',1) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('','@',-1) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('joeuser@mydatabase','',1) AS "joeuser@mydatabase";
- joeuser@mydatabase
- --------------------
- joeuser@mydatabase
- (1 row)
- select split_part('joeuser@mydatabase','',2) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('joeuser@mydatabase','',-1) AS "joeuser@mydatabase";
- joeuser@mydatabase
- --------------------
- joeuser@mydatabase
- (1 row)
- select split_part('joeuser@mydatabase','',-2) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('joeuser@mydatabase','@',0) AS "an error";
- ERROR: field position must not be zero
- select split_part('joeuser@mydatabase','@@',1) AS "joeuser@mydatabase";
- joeuser@mydatabase
- --------------------
- joeuser@mydatabase
- (1 row)
- select split_part('joeuser@mydatabase','@@',2) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('joeuser@mydatabase','@',1) AS "joeuser";
- joeuser
- ---------
- joeuser
- (1 row)
- select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
- mydatabase
- ------------
- mydatabase
- (1 row)
- select split_part('joeuser@mydatabase','@',3) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
- joeuser
- ---------
- joeuser
- (1 row)
- select split_part('joeuser@mydatabase','@',-1) AS "mydatabase";
- mydatabase
- ------------
- mydatabase
- (1 row)
- select split_part('joeuser@mydatabase','@',-2) AS "joeuser";
- joeuser
- ---------
- joeuser
- (1 row)
- select split_part('joeuser@mydatabase','@',-3) AS "empty string";
- empty string
- --------------
-
- (1 row)
- select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
- mydatabase
- ------------
- mydatabase
- (1 row)
- --
- -- test to_hex
- --
- select to_hex(256*256*256 - 1) AS "ffffff";
- ffffff
- --------
- ffffff
- (1 row)
- select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
- ffffffff
- ----------
- ffffffff
- (1 row)
- --
- -- MD5 test suite - from IETF RFC 1321
- -- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
- --
- select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
- TRUE
- ------
- t
- (1 row)
- --
- -- SHA-2
- --
- SET bytea_output TO hex;
- SELECT sha224('');
- sha224
- ------------------------------------------------------------
- \xd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
- (1 row)
- SELECT sha224('The quick brown fox jumps over the lazy dog.');
- sha224
- ------------------------------------------------------------
- \x619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c
- (1 row)
- SELECT sha256('');
- sha256
- --------------------------------------------------------------------
- \xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
- (1 row)
- SELECT sha256('The quick brown fox jumps over the lazy dog.');
- sha256
- --------------------------------------------------------------------
- \xef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c
- (1 row)
- SELECT sha384('');
- sha384
- ----------------------------------------------------------------------------------------------------
- \x38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
- (1 row)
- SELECT sha384('The quick brown fox jumps over the lazy dog.');
- sha384
- ----------------------------------------------------------------------------------------------------
- \xed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7
- (1 row)
- SELECT sha512('');
- sha512
- ------------------------------------------------------------------------------------------------------------------------------------
- \xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
- (1 row)
- SELECT sha512('The quick brown fox jumps over the lazy dog.');
- sha512
- ------------------------------------------------------------------------------------------------------------------------------------
- \x91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
- (1 row)
- --
- -- encode/decode
- --
- SELECT encode('\x1234567890abcdef00', 'hex');
- encode
- --------------------
- 1234567890abcdef00
- (1 row)
- SELECT decode('1234567890abcdef00', 'hex');
- decode
- ----------------------
- \x1234567890abcdef00
- (1 row)
- SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
- encode
- ------------------------------------------------------------------------------
- EjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN7wABEjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN+
- 7wABEjRWeJCrze8AAQ==
- (1 row)
- SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
- 'base64'), 'base64');
- decode
- ------------------------------------------------------------------------------------------------------------------------------------------------
- \x1234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef0001
- (1 row)
- SELECT encode('\x1234567890abcdef00', 'escape');
- encode
- -----------------------------
- \x124Vx\220\253\315\357\000
- (1 row)
- SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
- decode
- ----------------------
- \x1234567890abcdef00
- (1 row)
- --
- -- get_bit/set_bit etc
- --
- SELECT get_bit('\x1234567890abcdef00'::bytea, 43);
- get_bit
- ---------
- 1
- (1 row)
- SELECT get_bit('\x1234567890abcdef00'::bytea, 99); -- error
- ERROR: index 99 out of valid range, 0..71
- SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0);
- set_bit
- ----------------------
- \x1234567890a3cdef00
- (1 row)
- SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0); -- error
- ERROR: index 99 out of valid range, 0..71
- SELECT get_byte('\x1234567890abcdef00'::bytea, 3);
- get_byte
- ----------
- 120
- (1 row)
- SELECT get_byte('\x1234567890abcdef00'::bytea, 99); -- error
- ERROR: index 99 out of valid range, 0..8
- SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11);
- set_byte
- ----------------------
- \x1234567890abcd0b00
- (1 row)
- SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11); -- error
- ERROR: index 99 out of valid range, 0..8
- --
- -- test behavior of escape_string_warning and standard_conforming_strings options
- --
- set escape_string_warning = off;
- set standard_conforming_strings = off;
- show escape_string_warning;
- escape_string_warning
- -----------------------
- off
- (1 row)
- show standard_conforming_strings;
- standard_conforming_strings
- -----------------------------
- off
- (1 row)
- set escape_string_warning = on;
- set standard_conforming_strings = on;
- show escape_string_warning;
- escape_string_warning
- -----------------------
- on
- (1 row)
- show standard_conforming_strings;
- standard_conforming_strings
- -----------------------------
- on
- (1 row)
- select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6;
- f1 | f2 | f3 | f4 | f5 | f6
- -------+--------+---------+-------+--------+----
- a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
- (1 row)
- set standard_conforming_strings = off;
- select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
- WARNING: nonstandard use of \\ in a string literal
- LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
- ^
- HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
- WARNING: nonstandard use of \\ in a string literal
- LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
- ^
- HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
- WARNING: nonstandard use of \\ in a string literal
- LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
- ^
- HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
- WARNING: nonstandard use of \\ in a string literal
- LINE 1: ...bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' ...
- ^
- HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
- WARNING: nonstandard use of \\ in a string literal
- LINE 1: ...'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd'...
- ^
- HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
- WARNING: nonstandard use of \\ in a string literal
- LINE 1: ...'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as ...
- ^
- HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
- f1 | f2 | f3 | f4 | f5 | f6
- -------+--------+---------+-------+--------+----
- a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
- (1 row)
- set escape_string_warning = off;
- set standard_conforming_strings = on;
- select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6;
- f1 | f2 | f3 | f4 | f5 | f6
- -------+--------+---------+-------+--------+----
- a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
- (1 row)
- set standard_conforming_strings = off;
- select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
- f1 | f2 | f3 | f4 | f5 | f6
- -------+--------+---------+-------+--------+----
- a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
- (1 row)
- reset standard_conforming_strings;
- --
- -- Additional string functions
- --
- SET bytea_output TO escape;
- SELECT initcap('hi THOMAS');
- initcap
- -----------
- Hi Thomas
- (1 row)
- SELECT lpad('hi', 5, 'xy');
- lpad
- -------
- xyxhi
- (1 row)
- SELECT lpad('hi', 5);
- lpad
- -------
- hi
- (1 row)
- SELECT lpad('hi', -5, 'xy');
- lpad
- ------
-
- (1 row)
- SELECT lpad('hello', 2);
- lpad
- ------
- he
- (1 row)
- SELECT lpad('hi', 5, '');
- lpad
- ------
- hi
- (1 row)
- SELECT rpad('hi', 5, 'xy');
- rpad
- -------
- hixyx
- (1 row)
- SELECT rpad('hi', 5);
- rpad
- -------
- hi
- (1 row)
- SELECT rpad('hi', -5, 'xy');
- rpad
- ------
-
- (1 row)
- SELECT rpad('hello', 2);
- rpad
- ------
- he
- (1 row)
- SELECT rpad('hi', 5, '');
- rpad
- ------
- hi
- (1 row)
- SELECT ltrim('zzzytrim', 'xyz');
- ltrim
- -------
- trim
- (1 row)
- SELECT translate('', '14', 'ax');
- translate
- -----------
-
- (1 row)
- SELECT translate('12345', '14', 'ax');
- translate
- -----------
- a23x5
- (1 row)
- SELECT ascii('x');
- ascii
- -------
- 120
- (1 row)
- SELECT ascii('');
- ascii
- -------
- 0
- (1 row)
- SELECT chr(65);
- chr
- -----
- A
- (1 row)
- SELECT chr(0);
- ERROR: null character not permitted
- SELECT repeat('Pg', 4);
- repeat
- ----------
- PgPgPgPg
- (1 row)
- SELECT repeat('Pg', -4);
- repeat
- --------
-
- (1 row)
- SELECT SUBSTRING('1234567890'::bytea FROM 3) "34567890";
- 34567890
- ----------
- 34567890
- (1 row)
- SELECT SUBSTRING('1234567890'::bytea FROM 4 FOR 3) AS "456";
- 456
- -----
- 456
- (1 row)
- SELECT SUBSTRING('string'::bytea FROM 2 FOR 2147483646) AS "tring";
- tring
- -------
- tring
- (1 row)
- SELECT SUBSTRING('string'::bytea FROM -10 FOR 2147483646) AS "string";
- string
- --------
- string
- (1 row)
- SELECT SUBSTRING('string'::bytea FROM -10 FOR -2147483646) AS "error";
- ERROR: negative substring length not allowed
- SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea);
- btrim
- -------
- Tom
- (1 row)
- SELECT trim(leading E'\\000'::bytea from E'\\000Tom\\000'::bytea);
- ltrim
- ---------
- Tom\000
- (1 row)
- SELECT trim(trailing E'\\000'::bytea from E'\\000Tom\\000'::bytea);
- rtrim
- ---------
- \000Tom
- (1 row)
- SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea);
- btrim
- -------
- trim
- (1 row)
- SELECT btrim(''::bytea, E'\\000'::bytea);
- btrim
- -------
-
- (1 row)
- SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea);
- btrim
- --------------
- \000trim\000
- (1 row)
- SELECT encode(overlay(E'Th\\000omas'::bytea placing E'Th\\001omas'::bytea from 2),'escape');
- encode
- -------------
- TTh\x01omas
- (1 row)
- SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 8),'escape');
- encode
- --------------------
- Th\000omas\x02\x03
- (1 row)
- SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 5 for 3),'escape');
- encode
- -----------------
- Th\000o\x02\x03
- (1 row)
- SELECT bit_count('\x1234567890'::bytea);
- bit_count
- -----------
- 15
- (1 row)
- SELECT unistr('\0064at\+0000610');
- unistr
- --------
- data0
- (1 row)
- SELECT unistr('d\u0061t\U000000610');
- unistr
- --------
- data0
- (1 row)
- SELECT unistr('a\\b');
- unistr
- --------
- a\b
- (1 row)
- -- errors:
- SELECT unistr('wrong: \db99');
- ERROR: invalid Unicode surrogate pair
- SELECT unistr('wrong: \db99\0061');
- ERROR: invalid Unicode surrogate pair
- SELECT unistr('wrong: \+00db99\+000061');
- ERROR: invalid Unicode surrogate pair
- SELECT unistr('wrong: \+2FFFFF');
- ERROR: invalid Unicode code point: 2FFFFF
- SELECT unistr('wrong: \udb99\u0061');
- ERROR: invalid Unicode surrogate pair
- SELECT unistr('wrong: \U0000db99\U00000061');
- ERROR: invalid Unicode surrogate pair
- SELECT unistr('wrong: \U002FFFFF');
- ERROR: invalid Unicode code point: 2FFFFF
- SELECT unistr('wrong: \xyz');
- ERROR: invalid Unicode escape
- HINT: Unicode escapes must be \XXXX, \+XXXXXX, \uXXXX, or \UXXXXXXXX.
|