首页 > 解决方案 > Why is SPLIT_PART working when I create a view but not when updating a table column?

问题描述

I've been using SPLIT_PART when creating views without issues and just tried updating a column in a table using the same syntax but am getting this error:

SQL Error [42601]: Unterminated string literal started at position 0 in SQL ', 0)). Expected char

From the following sql:

update table1
    set email1 = split_part(emails_combined,';',1);

Why does this work when creating a view but not when updating a column in a table?

Is there a way I can get this to work in table? I have a column with a list of emails separated by semicolons that I want to split and fill separate columns with each email.

Update -- Here is a sample of the data in the column I'm trying to split:

    testing@123.net;    
    ;jon@loremipsum.com;jason@sample.net;test@123.com
    jon@loremipsum.com;jason@sample.net;test@123.com
    ;testing@123.net
    ;info@loremipsum.com;misc@random.com;info@stuff.com
    ;  
    jon@loremipsum.com;jason@sample.net;test@123.com

There are rows where the only value is a ';'. There are no null rows.

Update: I randomly tried moving the closing ; down another line and the SQL worked. Like this:

   update table1
    set email1 = split_part(emails_combined,';',1)
;

Why would moving the closing semicolon like this make a difference?

标签: postgresqlviewsyntax-error

解决方案


推荐阅读