如何为结尾为 space 的 char(n) 类型创建 StartsWith 函数
How to create StartsWith function for char(n) type with ending space
我需要创建 startswith 函数,如果 char(n) 数据库,该函数 return 为真
列以一些字符开头
末尾可以包含 space。
空格应该像其他字符一样对待。
数据库有两个值“A”和“AA”。我希望 startwith('A')(不带尾随 space)与 AA 和 A 匹配,但 startwith('A ')(带尾随 space)仅与 A 匹配。
使用下面的示例数据
startswith( test, 'A') -- works
startswith( test, 'A ') -- returns wrong result : false
StartsWith(test, rpad('A',20) ) -- returns wrong result : false
应该return正确
但是
startswith( test, RPAD( 'A', 21))
应该return false,因为检查字符串末尾有额外的space。
数据库包含具有 char(20) 类型列的测试列,这不能
改变了。
我尝试了下面的代码,但它 return 是错误的。
如何解决这个问题以使其 return 成立?
从 9.1
开始使用 Postgres
安德鲁斯
CREATE or replace FUNCTION public.likeescape( str text )
--
RETURNS text AS $$
SELECT replace(replace(replace(,'^','^^'),'%','^%'),'_','^_') ;
$$ LANGUAGE sql IMMUTABLE;
CREATE or replace FUNCTION public.StartWith( cstr text, algusosa text )
RETURNS bool AS $$
SELECT is null or like likeescape() ||'%' ESCAPE '^' ;
$$ LANGUAGE sql IMMUTABLE;
create temp table test ( test char(20) ) on commit drop;
insert into test values ('A' );
insert into test values ('AA' );
select StartWith(test, 'A ' ) from test
我也将其发布到 pgsql-general 邮件列表。
Values of type character
are physically padded with spaces to the specified width n, and are stored and displayed that way. However, the padding spaces are treated as semantically insignificant. Trailing spaces are disregarded when comparing two values of type character
, and they will be removed when converting a character
value to one of the other string types. Note that trailing spaces are semantically significant in character varying
and text
values, and when using pattern matching, e.g. LIKE
, regular expressions.
请注意,您函数的两个参数都是 text
。当您传递它时,列 test
和文字 'A '
、test
会在隐式转换时丢失尾随空格,而文字不会。在你的函数中你最终会得到类似
的东西
'A' LIKE 'A %' ESCAPE '^'
这不是真的。
您可以重载您的函数并创建它的副本,其中第一个参数是 char
或者简单地使用正则表达式而不是定义您自己的函数,例如 test ~ '^A '
或 rtrim()
char
之外的空格并将其视为 "spaceless" 与 rtrim(test) LIKE 'A%'
一样。我更喜欢后者之一。
我需要创建 startswith 函数,如果 char(n) 数据库,该函数 return 为真 列以一些字符开头 末尾可以包含 space。 空格应该像其他字符一样对待。
数据库有两个值“A”和“AA”。我希望 startwith('A')(不带尾随 space)与 AA 和 A 匹配,但 startwith('A ')(带尾随 space)仅与 A 匹配。
使用下面的示例数据
startswith( test, 'A') -- works
startswith( test, 'A ') -- returns wrong result : false
StartsWith(test, rpad('A',20) ) -- returns wrong result : false
应该return正确
但是
startswith( test, RPAD( 'A', 21))
应该return false,因为检查字符串末尾有额外的space。
数据库包含具有 char(20) 类型列的测试列,这不能 改变了。
我尝试了下面的代码,但它 return 是错误的。
如何解决这个问题以使其 return 成立? 从 9.1
开始使用 Postgres安德鲁斯
CREATE or replace FUNCTION public.likeescape( str text )
--
RETURNS text AS $$
SELECT replace(replace(replace(,'^','^^'),'%','^%'),'_','^_') ;
$$ LANGUAGE sql IMMUTABLE;
CREATE or replace FUNCTION public.StartWith( cstr text, algusosa text )
RETURNS bool AS $$
SELECT is null or like likeescape() ||'%' ESCAPE '^' ;
$$ LANGUAGE sql IMMUTABLE;
create temp table test ( test char(20) ) on commit drop;
insert into test values ('A' );
insert into test values ('AA' );
select StartWith(test, 'A ' ) from test
我也将其发布到 pgsql-general 邮件列表。
Values of type
character
are physically padded with spaces to the specified width n, and are stored and displayed that way. However, the padding spaces are treated as semantically insignificant. Trailing spaces are disregarded when comparing two values of typecharacter
, and they will be removed when converting acharacter
value to one of the other string types. Note that trailing spaces are semantically significant incharacter varying
andtext
values, and when using pattern matching, e.g.LIKE
, regular expressions.
请注意,您函数的两个参数都是 text
。当您传递它时,列 test
和文字 'A '
、test
会在隐式转换时丢失尾随空格,而文字不会。在你的函数中你最终会得到类似
'A' LIKE 'A %' ESCAPE '^'
这不是真的。
您可以重载您的函数并创建它的副本,其中第一个参数是 char
或者简单地使用正则表达式而不是定义您自己的函数,例如 test ~ '^A '
或 rtrim()
char
之外的空格并将其视为 "spaceless" 与 rtrim(test) LIKE 'A%'
一样。我更喜欢后者之一。