1997-11-30 21:48:47 -05:00
|
|
|
--
|
2000-01-05 01:07:58 -05:00
|
|
|
-- STRINGS
|
1997-11-30 21:48:47 -05:00
|
|
|
-- Test various data entry syntaxes.
|
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
-- SQL92 string continuation syntax
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E021-03 character string literals
|
1997-11-30 21:48:47 -05:00
|
|
|
SELECT 'first line'
|
|
|
|
|
' - next line'
|
|
|
|
|
' - third line'
|
|
|
|
|
AS "Three lines to one";
|
|
|
|
|
|
|
|
|
|
-- illegal string continuation syntax
|
|
|
|
|
SELECT 'first line'
|
|
|
|
|
' - next line' /* this comment is not allowed here */
|
1998-05-29 09:23:02 -04:00
|
|
|
' - third line'
|
|
|
|
|
AS "Illegal comment within continuation";
|
1997-11-30 21:48:47 -05:00
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test conversions between various string types
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E021-10 implicit casting among the character data types
|
1997-11-30 21:48:47 -05:00
|
|
|
--
|
|
|
|
|
|
1998-05-29 09:23:02 -04:00
|
|
|
SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
|
1997-11-30 21:48:47 -05:00
|
|
|
|
1998-05-29 09:23:02 -04:00
|
|
|
SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
|
|
|
|
|
|
|
|
|
|
SELECT CAST(name 'namefield' AS text) AS "text(name)";
|
|
|
|
|
|
2001-05-21 12:54:46 -04:00
|
|
|
SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL; -- fail
|
|
|
|
|
|
|
|
|
|
SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-01-16 19:16:41 -05:00
|
|
|
SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-01-16 19:16:41 -05:00
|
|
|
SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
|
|
|
|
SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
|
|
|
|
|
|
|
|
|
|
SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
|
|
|
|
|
|
|
|
|
|
SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test SQL92 string functions
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E### and T### are feature reference numbers from SQL99
|
1998-05-29 09:23:02 -04:00
|
|
|
--
|
|
|
|
|
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E021-09 trim function
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT TRIM(BOTH FROM ' bunch o blanks ') = 'bunch o blanks' AS "bunch o blanks";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT TRIM(LEADING FROM ' bunch o blanks ') = 'bunch o blanks ' AS "bunch o blanks ";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT TRIM(TRAILING FROM ' bunch o blanks ') = ' bunch o blanks' AS " bunch o blanks";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E021-06 substring expression
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2002-06-11 11:44:38 -04:00
|
|
|
-- T581 regular expression substring
|
|
|
|
|
SELECT SUBSTRING('abcdefg' FROM '(b|f).*(d)' FOR '#') AS "bcd";
|
|
|
|
|
|
|
|
|
|
-- No match should return NULL
|
|
|
|
|
SELECT SUBSTRING('abcdefg' FROM '(1|2|3)' FOR '#') IS NULL AS "True";
|
|
|
|
|
|
|
|
|
|
-- Null inputs should return NULL
|
|
|
|
|
SELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True";
|
|
|
|
|
SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True";
|
|
|
|
|
SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True";
|
|
|
|
|
|
|
|
|
|
-- PostgreSQL extention to allow omitting the escape character
|
|
|
|
|
SELECT SUBSTRING('abcdefg' FROM '(c|d).e') AS "cde";
|
|
|
|
|
|
|
|
|
|
-- E021-11 position expression
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT POSITION('4' IN '1234567890') = '4' AS "4";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT POSITION(5 IN '1234567890') = '5' AS "5";
|
|
|
|
|
|
2002-06-11 11:44:38 -04:00
|
|
|
-- T312 character overlay function
|
|
|
|
|
SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
|
|
|
|
|
|
|
|
|
|
SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
|
|
|
|
|
|
|
|
|
|
SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
|
|
|
|
|
|
|
|
|
|
SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
|
|
|
|
|
|
2000-08-06 14:13:42 -04:00
|
|
|
--
|
|
|
|
|
-- test LIKE
|
|
|
|
|
-- Be sure to form every test as a LIKE/NOT LIKE pair.
|
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
-- simplest examples
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E061-04 like predicate
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT 'hawkeye' LIKE 'h%' AS "true";
|
|
|
|
|
SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'hawkeye' LIKE 'H%' AS "false";
|
|
|
|
|
SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
|
|
|
|
|
|
|
|
|
|
SELECT 'hawkeye' LIKE 'indio%' AS "false";
|
|
|
|
|
SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
|
|
|
|
|
|
|
|
|
|
SELECT 'hawkeye' LIKE 'h%eye' AS "true";
|
|
|
|
|
SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'indio' LIKE '_ndio' AS "true";
|
|
|
|
|
SELECT 'indio' NOT LIKE '_ndio' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'indio' LIKE 'in__o' AS "true";
|
|
|
|
|
SELECT 'indio' NOT LIKE 'in__o' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'indio' LIKE 'in_o' AS "false";
|
|
|
|
|
SELECT 'indio' NOT LIKE 'in_o' AS "true";
|
|
|
|
|
|
|
|
|
|
-- unused escape character
|
|
|
|
|
SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
|
|
|
|
|
SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
|
|
|
|
|
SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
|
|
|
|
|
|
|
|
|
|
-- escape character
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E061-05 like predicate with escape clause
|
2000-08-06 14:13:42 -04:00
|
|
|
SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
|
|
|
|
|
SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
|
|
|
|
|
SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
|
|
|
|
|
|
|
|
|
|
SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
|
|
|
|
|
SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
|
|
|
|
|
SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
|
|
|
|
|
SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
|
|
|
|
|
SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
|
|
|
|
|
SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
|
|
|
|
|
|
|
|
|
|
SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
|
|
|
|
|
SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
|
|
|
|
|
|
|
|
|
|
-- escape character same as pattern character
|
|
|
|
|
SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
|
|
|
|
|
SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
|
|
|
|
|
SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
|
|
|
|
|
SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
|
|
|
|
|
SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
|
|
|
|
|
SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test ILIKE (case-insensitive LIKE)
|
|
|
|
|
-- Be sure to form every test as an ILIKE/NOT ILIKE pair.
|
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
SELECT 'hawkeye' ILIKE 'h%' AS "true";
|
|
|
|
|
SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'hawkeye' ILIKE 'H%' AS "true";
|
|
|
|
|
SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
|
|
|
|
|
SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
|
|
|
|
|
|
|
|
|
|
SELECT 'Hawkeye' ILIKE 'h%' AS "true";
|
|
|
|
|
SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test implicit type conversion
|
|
|
|
|
--
|
|
|
|
|
|
2002-06-11 11:44:38 -04:00
|
|
|
-- E021-07 character concatenation
|
1998-05-29 09:23:02 -04:00
|
|
|
SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
|
|
|
|
|
|
|
|
|
|
SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
|
|
|
|
|
|
2001-06-01 13:49:17 -04:00
|
|
|
SELECT char(20) 'characters' || 'and text' AS "Concat char to unknown type";
|
|
|
|
|
|
2001-05-21 12:54:46 -04:00
|
|
|
SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
|
1998-05-29 09:23:02 -04:00
|
|
|
|
|
|
|
|
SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
|
2002-08-21 23:24:01 -04:00
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- 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));
|
|
|
|
|
|
2002-08-28 16:18:29 -04:00
|
|
|
--
|
|
|
|
|
-- 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));
|
|
|
|
|
|
2002-08-21 23:24:01 -04:00
|
|
|
-- 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 SQL92.
|
|
|
|
|
SELECT substr(f1, -1, 5) from toasttest;
|
|
|
|
|
|
|
|
|
|
-- If the length is less than zero, an ERROR is thrown.
|
|
|
|
|
SELECT substr(f1, 5, -1) from toasttest;
|
|
|
|
|
|
|
|
|
|
-- If no third argument (length) is provided, the length to the end of the
|
|
|
|
|
-- string is assumed.
|
|
|
|
|
SELECT substr(f1, 99995) from toasttest;
|
|
|
|
|
|
|
|
|
|
-- If start plus length is > string length, the result is truncated to
|
|
|
|
|
-- string length
|
|
|
|
|
SELECT substr(f1, 99995, 10) from toasttest;
|
|
|
|
|
|
|
|
|
|
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'));
|
|
|
|
|
|
2002-08-28 16:18:29 -04:00
|
|
|
--
|
|
|
|
|
-- 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'));
|
|
|
|
|
|
2002-08-21 23:24:01 -04:00
|
|
|
-- 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 SQL92.
|
|
|
|
|
SELECT substr(f1, -1, 5) from toasttest;
|
|
|
|
|
|
|
|
|
|
-- If the length is less than zero, an ERROR is thrown.
|
|
|
|
|
SELECT substr(f1, 5, -1) from toasttest;
|
|
|
|
|
|
|
|
|
|
-- If no third argument (length) is provided, the length to the end of the
|
|
|
|
|
-- string is assumed.
|
|
|
|
|
SELECT substr(f1, 99995) from toasttest;
|
|
|
|
|
|
|
|
|
|
-- If start plus length is > string length, the result is truncated to
|
|
|
|
|
-- string length
|
|
|
|
|
SELECT substr(f1, 99995, 10) from toasttest;
|
|
|
|
|
|
|
|
|
|
DROP TABLE toasttest;
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test length
|
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
SELECT length('abcdef') AS "length_6";
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test strpos
|
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
SELECT strpos('abcdef', 'cd') AS "pos_3";
|
|
|
|
|
|
|
|
|
|
SELECT strpos('abcdef', 'xy') AS "pos_0";
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test replace
|
|
|
|
|
--
|
|
|
|
|
SELECT replace('abcdef', 'de', '45') AS "abc45f";
|
|
|
|
|
|
|
|
|
|
SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
|
|
|
|
|
|
|
|
|
|
SELECT replace('yabadoo', 'bad', '') AS "yaoo";
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test split
|
|
|
|
|
--
|
|
|
|
|
select split('joeuser@mydatabase','@',0) AS "an error";
|
|
|
|
|
|
|
|
|
|
select split('joeuser@mydatabase','@',1) AS "joeuser";
|
|
|
|
|
|
|
|
|
|
select split('joeuser@mydatabase','@',2) AS "mydatabase";
|
|
|
|
|
|
|
|
|
|
select split('joeuser@mydatabase','@',3) AS "empty string";
|
|
|
|
|
|
|
|
|
|
select split('@joeuser@mydatabase@','@',2) AS "joeuser";
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
|
-- test to_hex
|
|
|
|
|
--
|
|
|
|
|
select to_hex(256*256*256 - 1) AS "ffffff";
|
|
|
|
|
|
|
|
|
|
select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
|