0

I have a query:

SELECT phone, 
       to_char(appt_date,'MM/DD/YYYY'), 
       time_text, 
       staff_email || subject_line as title,
       staff_wav,
       agency_wav 
FROM phone_dialer

that is sent to a csv file

That results in

9105554444,07/01/2011,08:30am,me@myemail.orgGADK082493,staffid0827,Coastal Counseling

or

9105554444,07/01/2011,08:30am,me@myemail.orgGADK082493,staffid0827,Smith, Bob

The "agency_wav" column could have a name of company. I have tried several ways to remove the comma between Smith, Bob and am failing miserably.

Can anyone steer me to a resolution?

4

3 回答 3

3

回答标题,因为问题的主体不清楚。

从字符串中删除逗号的最快方法:

SELECT translate('Smith, Bob, foo,,,bar', ',','')

相关答案寻址translate()/replace()

于 2013-10-25T18:47:12.590 回答
1

创建 CSV 的首选方法是使用 COPY 命令。

如果由于某种原因您不想要或不能使用它,您只需在 CSV 友好列中返回值,该列将值括在双引号中,并通过在字符串中复制现有的双引号来转义它们。这将保留正确的值(即所有逗号),但不会破坏 CSV 格式。

SELECT phone, 
       to_char(appt_date,'MM/DD/YYYY'), 
       time_text, 
       staff_email || subject_line as title,
       staff_wav,
       '"' || replace(agency_wav, '"', '""') || '"'
FROM phone_dialer

这将产生以下行

9105554444,07/01/2011,08:30am,me@myemail.orgGADK082493,staffid0827,"Smith, Bob"

注意带逗号的引用值。

于 2013-10-26T17:21:54.677 回答
1

如果您使用语法 COPY () TO STDOUT WITH CSV 包围您的查询;然后它将构造 CSV 输出并自动引用包含逗号的字段值。

如果您想在查询中手动执行此操作,请尝试 replace(agency_wav,',','')。

于 2013-10-25T18:47:08.870 回答