2015-04-08 34 views
1

array_to_string返回text(916-555-1212),但是即使使用显式::文本转换,postgresql也将其视为设置操作。即使没有设置,NULLIF也不支持设置参数错误

select nullif(
    array_to_string( 
    regexp_matches('9165551212', '(\d{3})?(\d{3})(\d{4})')::text[] 
    ,'-')::text 
    , ''); 
ERROR: NULLIF does not support set arguments 

但我可以使用CHAR_LENGTH其预计文本和它的作品

select char_length(
    array_to_string( 
    regexp_matches('9165551212', '(\d{3})?(\d{3})(\d{4})')::text[] 
    ,'-')::text 
) 
char_length 
------------- 
     12 

但包装甚至在NULLIF和同样的错误

select nullif(
    char_length(
    array_to_string( 
     regexp_matches('9165551212', '(\d{3})?(\d{3})(\d{4})')::text[] 
    ,'-')::text 
) 
    ,12) 
ERROR: NULLIF does not support set arguments 
+0

[从手册] (http://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP)“*函数可以不返回行,一行或多行*” - 因为' regexp_matches'被标记为一个可能_can_返回多行的函数,你会得到这个错误(即使在你的情况下它不会返回多行) –

+0

[也在手册中;)](http://www.postgresql.org /docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP)“可以forc e regexp_matches()总是通过使用子选择返回一行;当你想要返回所有的行,甚至是不匹配的行时,这在SELECT目标列表中特别有用“ – Krut

+0

你可以强制它返回一行,但这并不改变它被标记为可能被_able_返回多于一个(与例如'lower()'相反,这将**总是**返回单个值)。 –

回答

1

我有同样的问题,它似乎被相关regexp_matches功能:

select nullif(   (regexp_matches('123', '(2)')  )[1]  , '') 
; -- ERROR: NULLIF does not support set arguments 
select nullif(   (regexp_matches('123', '(2)')::text[])[1]::text, '') 
; -- ERROR: NULLIF does not support set arguments 
select nullif((select (regexp_matches('123', '(2)')  )[1] ), '') 
; -- ok: gives "2" 

所以只是包装的结果在子选择似乎在这里解决它,以及: -/

0

奇怪......它的工作原理,如果你做

select 
    nullif(
    (
    select array_to_string( 
     regexp_matches(
     '9165551212', 
     '(\d{3})?(\d{3})(\d{4})' 
     )::text[] 
     , '-') 
    ) 
    , '') 
相关问题