在PostgreSQL中使用带有变量的正则表达式的正确语法

Ala*_*yne 1 regex postgresql

PostgreSQL 9.5.4

我有以下功能,我试图使用正则表达式中的参数.就像是

CREATE OR REPLACE FUNCTION test(lastname text, firstname text, birthdate date)
  RETURNS SETOF view_patient AS
$BODY$ 

   select * from testing t
   where t.lastname ~*  '^' || $1 || ''
   order by t.lastname

$BODY$
  LANGUAGE sql VOLATILE;
Run Code Online (Sandbox Code Playgroud)

返回的错误是:

错误:WHERE的参数必须是boolean类型,而不是类型文本LINE 55:其中t.lastname~*'^'|| 1美元|| ""

这是怎么做到的?

TIA

a_h*_*ame 5

您需要在括号之间放置连接(并且您可以在末尾删除空字符串:

where t.lastname ~*  ('^' || $1) 
Run Code Online (Sandbox Code Playgroud)

或者:

where t.lastname ~*  concat('^', $1)
Run Code Online (Sandbox Code Playgroud)