strings.out

来自「postgresql8.3.4源码,开源数据库」· OUT 代码 · 共 1,243 行 · 第 1/3 页

OUT
1,243
字号
---- STRINGS-- Test various data entry syntaxes.---- SQL92 string continuation syntax-- E021-03 character string literalsSELECT '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 syntaxSELECT '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'        ^---- 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.sqlSELECT 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 SQL92 string functions-- E### and T### are feature reference numbers from SQL99---- E021-09 trim functionSELECT 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 expressionSELECT 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)-- T581 regular expression substring (with SQL99's bizarre regexp syntax)SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd"; bcd ----- bcd(1 row)-- No match should return NULLSELECT SUBSTRING('abcdefg' FROM '#"(b_d)#"%' FOR '#') IS NULL AS "True"; True ------ t(1 row)-- Null inputs should return NULLSELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True"; True ------ t(1 row)SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True"; True ------ t(1 row)SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True"; True ------ t(1 row)-- PostgreSQL extension to allow omitting the escape character;-- here the regexp is taken as Posix syntaxSELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde"; cde ----- cde(1 row)-- With a parenthesized subexpression, return only what matches the subexprSELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde"; cde ----- cde(1 row)-- PostgreSQL extension to allow using back reference in replace string;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 optionSELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');ERROR:  invalid regexp option: "z"-- set so we can tell NULL from empty string\pset null '\\N'-- return all matches from regexpSELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$); regexp_matches ---------------- {bar,beque}(1 row)-- test case insensitiveSELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i'); regexp_matches ---------------- {bAR,bEqUE}(1 row)-- global option - more than one matchSELECT 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 matchSELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$); regexp_matches ----------------(0 rows)-- optional capture group did not match, null entry in arraySELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);  regexp_matches  ------------------ {bar,NULL,beque}(1 row)-- no capture groupsSELECT regexp_matches('foobarbequebaz', $re$barbeque$re$); regexp_matches ---------------- {barbeque}(1 row)-- give me errorsSELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');ERROR:  invalid regexp option: "z"SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);ERROR:  invalid regular expression: parentheses () not balancedSELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);ERROR:  invalid regular expression: invalid repetition count(s)-- split string on regexpSELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', $re$\s+$re$) AS foo;  foo   | length --------+-------- the    |      3 quick  |      5 brown  |      5 fox    |      3 jumped |      6 over   |      4 the    |      3 lazy   |      4 dog    |      3(9 rows)SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', $re$\s+$re$);             regexp_split_to_array              ------------------------------------------------ {the,quick,brown,fox,jumped,over,the,lazy,dog}(1 row)SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped 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 e   |      1 d   |      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(36 rows)SELECT regexp_split_to_array('the quick brown fox jumped 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,e,d,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 jumped 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 e   |      1 d   |      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(44 rows)SELECT regexp_split_to_array('the quick brown fox jumped 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,e,d," ",o,v,e,r," ",t,h,e," ",l,a,z,y," ",d,o,g}(1 row)-- case insensitiveSELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'i') AS foo;          foo          | length -----------------------+-------- th                    |      2  QUick bROWn FOx jUMP |     21 d ov                  |      4 r TH                  |      4  lazy dOG             |      9(5 rows)SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'i');

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?