2015-04-08 35 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[] 
     , '-') 
    ) 
    , '') 
相關問題