]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/strings.sql
2095848a39dbea2d312c5c860c090964a5a439da
[postgresql] / src / test / regress / sql / strings.sql
1 --
2 -- STRINGS
3 -- Test various data entry syntaxes.
4 --
5
6 -- SQL92 string continuation syntax
7 -- E021-03 character string literals
8 SELECT 'first line'
9 ' - next line'
10         ' - third line'
11         AS "Three lines to one";
12
13 -- illegal string continuation syntax
14 SELECT 'first line'
15 ' - next line' /* this comment is not allowed here */
16 ' - third line'
17         AS "Illegal comment within continuation";
18
19 --
20 -- test conversions between various string types
21 -- E021-10 implicit casting among the character data types
22 --
23
24 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
25
26 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
27
28 SELECT CAST(name 'namefield' AS text) AS "text(name)";
29
30 SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL; -- fail
31
32 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
33
34 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
35
36 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
37
38 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
39
40 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
41
42 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
43
44 --
45 -- test SQL92 string functions
46 -- E### and T### are feature reference numbers from SQL99
47 --
48
49 -- E021-09 trim function
50 SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
51
52 SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
53
54 SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
55
56 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
57
58 -- E021-06 substring expression
59 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
60
61 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
62
63 -- T581 regular expression substring
64 SELECT SUBSTRING('abcdefg' FROM '(b|f).*(d)' FOR '#') AS "bcd";
65
66 -- No match should return NULL
67 SELECT SUBSTRING('abcdefg' FROM '(1|2|3)' FOR '#') IS NULL AS "True";
68
69 -- Null inputs should return NULL
70 SELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True";
71 SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True";
72 SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True";
73
74 -- PostgreSQL extention to allow omitting the escape character
75 SELECT SUBSTRING('abcdefg' FROM '(c|d).e') AS "cde";
76
77 -- E021-11 position expression
78 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
79
80 SELECT POSITION(5 IN '1234567890') = '5' AS "5";
81
82 -- T312 character overlay function
83 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
84
85 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
86
87 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
88
89 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
90
91 --
92 -- test LIKE
93 -- Be sure to form every test as a LIKE/NOT LIKE pair.
94 --
95
96 -- simplest examples
97 -- E061-04 like predicate
98 SELECT 'hawkeye' LIKE 'h%' AS "true";
99 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
100
101 SELECT 'hawkeye' LIKE 'H%' AS "false";
102 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
103
104 SELECT 'hawkeye' LIKE 'indio%' AS "false";
105 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
106
107 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
108 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
109
110 SELECT 'indio' LIKE '_ndio' AS "true";
111 SELECT 'indio' NOT LIKE '_ndio' AS "false";
112
113 SELECT 'indio' LIKE 'in__o' AS "true";
114 SELECT 'indio' NOT LIKE 'in__o' AS "false";
115
116 SELECT 'indio' LIKE 'in_o' AS "false";
117 SELECT 'indio' NOT LIKE 'in_o' AS "true";
118
119 -- unused escape character
120 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
121 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
122
123 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
124 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
125
126 -- escape character
127 -- E061-05 like predicate with escape clause
128 SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
129 SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
130
131 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
132 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
133
134 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
135 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
136
137 SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
138 SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
139
140 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
141 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
142
143 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
144 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
145
146 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
147 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
148
149 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
150 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
151
152 -- escape character same as pattern character
153 SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
154 SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
155
156 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
157 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
158
159 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
160 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
161
162 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
163 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
164
165 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
166 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
167
168
169 --
170 -- test ILIKE (case-insensitive LIKE)
171 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
172 --
173
174 SELECT 'hawkeye' ILIKE 'h%' AS "true";
175 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
176
177 SELECT 'hawkeye' ILIKE 'H%' AS "true";
178 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
179
180 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
181 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
182
183 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
184 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
185
186 --
187 -- test implicit type conversion
188 --
189
190 -- E021-07 character concatenation
191 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
192
193 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
194
195 SELECT char(20) 'characters' || 'and text' AS "Concat char to unknown type";
196
197 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
198
199 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
200
201 --
202 -- test substr with toasted text values
203 --
204 CREATE TABLE toasttest(f1 text);
205
206 insert into toasttest values(repeat('1234567890',10000));
207 insert into toasttest values(repeat('1234567890',10000));
208
209 --
210 -- Ensure that some values are uncompressed, to test the faster substring
211 -- operation used in that case
212 --
213 alter table toasttest alter column f1 set storage external;
214 insert into toasttest values(repeat('1234567890',10000));
215 insert into toasttest values(repeat('1234567890',10000));
216
217 -- If the starting position is zero or less, then return from the start of the string
218 -- adjusting the length to be consistent with the "negative start" per SQL92.
219 SELECT substr(f1, -1, 5) from toasttest;
220
221 -- If the length is less than zero, an ERROR is thrown.
222 SELECT substr(f1, 5, -1) from toasttest;
223
224 -- If no third argument (length) is provided, the length to the end of the
225 -- string is assumed.
226 SELECT substr(f1, 99995) from toasttest;
227
228 -- If start plus length is > string length, the result is truncated to
229 -- string length
230 SELECT substr(f1, 99995, 10) from toasttest;
231
232 DROP TABLE toasttest;
233
234 --
235 -- test substr with toasted bytea values
236 --
237 CREATE TABLE toasttest(f1 bytea);
238
239 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
240 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
241
242 --
243 -- Ensure that some values are uncompressed, to test the faster substring
244 -- operation used in that case
245 --
246 alter table toasttest alter column f1 set storage external;
247 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
248 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
249
250 -- If the starting position is zero or less, then return from the start of the string
251 -- adjusting the length to be consistent with the "negative start" per SQL92.
252 SELECT substr(f1, -1, 5) from toasttest;
253
254 -- If the length is less than zero, an ERROR is thrown.
255 SELECT substr(f1, 5, -1) from toasttest;
256
257 -- If no third argument (length) is provided, the length to the end of the
258 -- string is assumed.
259 SELECT substr(f1, 99995) from toasttest;
260
261 -- If start plus length is > string length, the result is truncated to
262 -- string length
263 SELECT substr(f1, 99995, 10) from toasttest;
264
265 DROP TABLE toasttest;
266
267 --
268 -- test length
269 --
270
271 SELECT length('abcdef') AS "length_6";
272
273 --
274 -- test strpos
275 --
276
277 SELECT strpos('abcdef', 'cd') AS "pos_3";
278
279 SELECT strpos('abcdef', 'xy') AS "pos_0";
280
281 --
282 -- test replace
283 --
284 SELECT replace('abcdef', 'de', '45') AS "abc45f";
285
286 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
287
288 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
289
290 --
291 -- test split
292 --
293 select split('joeuser@mydatabase','@',0) AS "an error";
294
295 select split('joeuser@mydatabase','@',1) AS "joeuser";
296
297 select split('joeuser@mydatabase','@',2) AS "mydatabase";
298
299 select split('joeuser@mydatabase','@',3) AS "empty string";
300
301 select split('@joeuser@mydatabase@','@',2) AS "joeuser";
302
303 --
304 -- test to_hex
305 --
306 select to_hex(256*256*256 - 1) AS "ffffff";
307
308 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";