SQL Plus将表转换为CSV文件问题

发布于 2024-09-29 19:07:04 字数 635 浏览 5 评论 0原文

我在将 Oracle 数据库中的表字段转换为 CSV 文件时遇到无法解决的问题。我有一个 varchar2(4000) 列,用户在其中输入他们的评论。有时,用户从其他程序(例如 MS Word)复制并粘贴文本,因此,许多额外信息被粘贴到字段中,例如回车符和换行符。该字段中也可能有大量文本。由于某种原因,即使我去掉了回车符和换行符,CSV 文件仍然显示 CR 和 LF,这导致当我在 Excel 中打开 CSV 文件时数据被分成两行。以下是我针对本专栏的代码。有人可以帮我弄清楚为什么会发生这种情况以及是否有办法解决这个问题?非常感谢任何帮助。谢谢。

SET RECSEP OFF 
SET TRIMOUT ON
SET TRIMSPOOL ON
SET TERMOUT OFF 
SET TERM OFF 
SET ECHO OFF 
SET VERIFY OFF 
SET HEAD OFF 
SET PAGESIZE 0 
SET LINESIZE 3000
SET COLSEP '|'  
SET FEEDBACK OFF
SET WRAP ON

spool 'C:\Temp\data.csv'

SELECT REPLACE (REPLACE (TRIM(COMMENTS), chr(13),''), chr(10), '') 
  FROM T_COMMENT;

SPOOL OFF

I'm having a problem that I can't figure out while converting a table field in an Oracle database to a CSV file. I have a column of varchar2(4000) where users enter their comments. Sometimes the users copy and paste text from other programs such as MS Word therefore, a lot of extra information is pasted into the field such as carriage returns and line feeds. There can also be large amount of text in this field. For some reason, even though I get rid of the carriage returns and line feeds, the CSV file still shows a CR and LF which causes the data to be separated into two rows when I open the CSV file in Excel. Below is my code for just this column. Can someone help me figure out why this is happening and if there is a way to fix the issue? Any help is much appreciated. Thanks.

SET RECSEP OFF 
SET TRIMOUT ON
SET TRIMSPOOL ON
SET TERMOUT OFF 
SET TERM OFF 
SET ECHO OFF 
SET VERIFY OFF 
SET HEAD OFF 
SET PAGESIZE 0 
SET LINESIZE 3000
SET COLSEP '|'  
SET FEEDBACK OFF
SET WRAP ON

spool 'C:\Temp\data.csv'

SELECT REPLACE (REPLACE (TRIM(COMMENTS), chr(13),''), chr(10), '') 
  FROM T_COMMENT;

SPOOL OFF

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-10-06 19:07:04

通常,如果 CSV 文件的数据中包含逗号或回车符,则该字段会用双引号引起来,以标识该字段跨越多行。

1,ABC,1
2,DEF,2
3,"G
H
I",3
4,JKL,4

我不确定其他角色的情况,但这是我看到的处理方式。

Typically, if a CSV file has commas or carriage-returns in the data then the field is double-quoted to identify that the field spans multiple lines.

1,ABC,1
2,DEF,2
3,"G
H
I",3
4,JKL,4

I'm not sure about the other characters, but this is the way I've seen this dealt with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文