1-- 2-- TEXT 3-- 4 5SELECT text 'this is a text string' = text 'this is a text string' AS true; 6 7SELECT text 'this is a text string' = text 'this is a text strin' AS false; 8 9CREATE TABLE TEXT_TBL (f1 text); 10 11INSERT INTO TEXT_TBL VALUES ('doh!'); 12INSERT INTO TEXT_TBL VALUES ('hi de ho neighbor'); 13 14SELECT '' AS two, * FROM TEXT_TBL; 15 16-- As of 8.3 we have removed most implicit casts to text, so that for example 17-- this no longer works: 18 19select length(42); 20 21-- But as a special exception for usability's sake, we still allow implicit 22-- casting to text in concatenations, so long as the other input is text or 23-- an unknown literal. So these work: 24 25select 'four: '::text || 2+2; 26select 'four: ' || 2+2; 27 28-- but not this: 29 30select 3 || 4.0; 31 32/* 33 * various string functions 34 */ 35select concat('one'); 36select concat(1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD')); 37select concat_ws('#','one'); 38select concat_ws('#',1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD')); 39select concat_ws(',',10,20,null,30); 40select concat_ws('',10,20,null,30); 41select concat_ws(NULL,10,20,null,30) is null; 42select reverse('abcde'); 43select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i; 44select quote_literal(''); 45select quote_literal('abc'''); 46select quote_literal(e'\\'); 47-- check variadic labeled argument 48select concat(variadic array[1,2,3]); 49select concat_ws(',', variadic array[1,2,3]); 50select concat_ws(',', variadic NULL::int[]); 51select concat(variadic NULL::int[]) is NULL; 52select concat(variadic '{}'::int[]) = ''; 53--should fail 54select concat_ws(',', variadic 10); 55 56/* 57 * format 58 */ 59select format(NULL); 60select format('Hello'); 61select format('Hello %s', 'World'); 62select format('Hello %%'); 63select format('Hello %%%%'); 64-- should fail 65select format('Hello %s %s', 'World'); 66select format('Hello %s'); 67select format('Hello %x', 20); 68-- check literal and sql identifiers 69select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello'); 70select format('%s%s%s','Hello', NULL,'World'); 71select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, NULL); 72select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', NULL, 'Hello'); 73-- should fail, sql identifier cannot be NULL 74select format('INSERT INTO %I VALUES(%L,%L)', NULL, 10, 'Hello'); 75-- check positional placeholders 76select format('%1$s %3$s', 1, 2, 3); 77select format('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); 78-- should fail 79select format('%1$s %4$s', 1, 2, 3); 80select format('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); 81select format('%0$s', 'Hello'); 82select format('%*0$s', 'Hello'); 83select format('%1$', 1); 84select format('%1$1', 1); 85-- check mix of positional and ordered placeholders 86select format('Hello %s %1$s %s', 'World', 'Hello again'); 87select format('Hello %s %s, %2$s %2$s', 'World', 'Hello again'); 88-- check variadic labeled arguments 89select format('%s, %s', variadic array['Hello','World']); 90select format('%s, %s', variadic array[1, 2]); 91select format('%s, %s', variadic array[true, false]); 92select format('%s, %s', variadic array[true, false]::text[]); 93-- check variadic with positional placeholders 94select format('%2$s, %1$s', variadic array['first', 'second']); 95select format('%2$s, %1$s', variadic array[1, 2]); 96-- variadic argument can be array type NULL, but should not be referenced 97select format('Hello', variadic NULL::int[]); 98-- variadic argument allows simulating more than FUNC_MAX_ARGS parameters 99select format(string_agg('%s',','), variadic array_agg(i)) 100from generate_series(1,200) g(i); 101-- check field widths and left, right alignment 102select format('>>%10s<<', 'Hello'); 103select format('>>%10s<<', NULL); 104select format('>>%10s<<', ''); 105select format('>>%-10s<<', ''); 106select format('>>%-10s<<', 'Hello'); 107select format('>>%-10s<<', NULL); 108select format('>>%1$10s<<', 'Hello'); 109select format('>>%1$-10I<<', 'Hello'); 110select format('>>%2$*1$L<<', 10, 'Hello'); 111select format('>>%2$*1$L<<', 10, NULL); 112select format('>>%2$*1$L<<', -10, NULL); 113select format('>>%*s<<', 10, 'Hello'); 114select format('>>%*1$s<<', 10, 'Hello'); 115select format('>>%-s<<', 'Hello'); 116select format('>>%10L<<', NULL); 117select format('>>%2$*1$L<<', NULL, 'Hello'); 118select format('>>%2$*1$L<<', 0, 'Hello'); 119