在 Unix 脚本中运行的 sqlplus 的 csv 输出格式

发布于 2024-08-10 20:14:22 字数 124 浏览 4 评论 0原文

我需要格式化 sqlplus 查询的 csv 输出文件。 char 和 varchar 输出字段以表中的最大列大小显示,这意味着很多列都有额外的空格以及逗号分隔符。如何去掉 csv 文件中多余的空白?另外,如何在数字字段周围获得“”?

I need to format csv output file from a sqlplus query. The char and varchar output fields come out in the max column size in the table, which means a lot of columns have extra blanks, plus the comma seperator. How can I get rid of the extra blanks in my csv file? Also how can I get "" around the numeric fields?

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

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

发布评论

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

评论(2

冷弦 2024-08-17 20:14:22

您必须将这些字段连接在一起,并自己添加引号。另外,不要忘记转义数据中的任何双引号。除非您的数据包含逗号或双引号,否则双引号并不是绝对必要的,但添加它们也没有什么坏处。

SELECT '"' || numcol || '",',
       '"' || REPLACE(strcol0, '"', '""') || '",',
       '"' || REPLACE(strcol1, '"', '""') || '"'
  FROM some_table;

在运行此命令之前,您可能需要关闭标题和反馈,然后后台打印到文件。

SQL> set heading off
SQL> set feedback off
SQL> set pagesize 0
SQL> spool output.csv
SQL> SELECT ... FROM ...;
SQL> spool off

You'll have to concat the fields together, and add the quotes yourself. Also, don't forget to escape any double quotes in your data. The double quotes aren't strictly necessary unless your data contains commas or double quotes, but it doesn't hurt to add them.

SELECT '"' || numcol || '",',
       '"' || REPLACE(strcol0, '"', '""') || '",',
       '"' || REPLACE(strcol1, '"', '""') || '"'
  FROM some_table;

Before you run this you'll probably want to turn headings and feedback off, then spool to a file.

SQL> set heading off
SQL> set feedback off
SQL> set pagesize 0
SQL> spool output.csv
SQL> SELECT ... FROM ...;
SQL> spool off
青萝楚歌 2024-08-17 20:14:22

您可以使用修剪功能去除前导/尾随空格:

http:// www.adp-gmbh.ch/ora/sql/trim.html

SELECT TRIM(col) FROM table;

不确定为什么您在数字字段周围得到引号:可能与您将列连接在一起的方式有关?

正如 D.Shawley 所问,如果您可以在此处发布一些用于生成 CSV 的 SQL 示例,将会有所帮助。

You can get rid of leading/trailing spaces by using trim function:

http://www.adp-gmbh.ch/ora/sql/trim.html

SELECT TRIM(col) FROM table;

Not sure why you are getting quotes around numeric fields : probably something to do with the way you are concating columns together ?

As D.Shawley asked - it would help if you could post some example SQL you are using to generate your CSV here.

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