oracle 中的文本换行

发布于 2024-10-19 02:02:11 字数 59 浏览 3 评论 0原文

我们如何才能仅在 ORACLE SQL 中将基于标准长度(例如 40 个字符)的文本(列值)包装成多行。

how can we wrap a text (coulmn value) basing ona stndard length of lets say 40 characters in to multi line in ORACLE SQL only.

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

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

发布评论

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

评论(3

枉心 2024-10-26 02:02:11
select regexp_replace(column_name, '(.{40})', '\1' || chr(10) || chr(13))
from some_table;
select regexp_replace(column_name, '(.{40})', '\1' || chr(10) || chr(13))
from some_table;
千仐 2024-10-26 02:02:11

在 SQL Plus 中,您可以指定列宽。

column column_name format a40 
select column_name from table_name

上面的输出格式为 40 列宽,并且会将所有内容包装到下一行。

输出显示通常由客户端控制

In SQL Plus you can assign column widths

column column_name format a40 
select column_name from table_name

The above format the output to be 40 columns wide and it would wrap anything to the next line.

Output display is usually controlled by the client

归途 2024-10-26 02:02:11

我从 Martin Burbridge 获得并修改了一些代码:

CREATE OR REPLACE FUNCTION line_wrap(p_str_to_wrap             VARCHAR2
                                    ,p_max_linesize            PLS_INTEGER
                                    ,p_indent_spaces_each_line PLS_INTEGER DEFAULT 0
                                    ,p_separator               VARCHAR2 DEFAULT ' ') RETURN VARCHAR2 IS
  -- This function will insert chr(10)'s (newlines) at the separator 
  --  nearest the specified linesize.
  -- The separator will default to a space if none provided.
  -- The p_indent_spaces_each_line parameter allows each line of wrapped text to be
  --  indented x spaces if desired. The indent_spaces will default to 0 if none provided. 
  v_max_linesize            PLS_INTEGER := nvl(p_max_linesize
                                              ,80);
  v_indent_spaces_each_line PLS_INTEGER := nvl(p_indent_spaces_each_line
                                              ,0);
  v_separator               VARCHAR2(20) := nvl(p_separator
                                               ,' ');
  v_str_to_wrap             VARCHAR2(4000) := p_str_to_wrap || v_separator;
  v_line                    VARCHAR2(4000);
  v_position                PLS_INTEGER;
  v_wrapped_text            VARCHAR2(4000);
  v_sql_errmsg              VARCHAR2(4000);

BEGIN
  WHILE v_str_to_wrap IS NOT NULL
  LOOP
    v_line     := substr(v_str_to_wrap
                        ,1
                        ,v_max_linesize);
    v_position := instr(v_line
                       ,v_separator
                       ,-1);
    IF v_position = 0
    THEN
      v_position := v_max_linesize;
    END IF;

    v_line := substr(v_line
                    ,1
                    ,v_position);

    IF v_indent_spaces_each_line > 0
    THEN
      v_wrapped_text := v_wrapped_text || chr(10) || lpad(' '
                                                         ,v_indent_spaces_each_line
                                                         ,' ') || v_line;
    ELSE
      v_wrapped_text := v_wrapped_text || chr(10) || v_line;
    END IF;

    v_str_to_wrap := substr(v_str_to_wrap
                           ,v_position + 1);
  END LOOP;

  RETURN v_wrapped_text;

EXCEPTION
  WHEN OTHERS THEN
    v_sql_errmsg := 'Error in word_wrap: ' || SQLERRM;
    raise_application_error(-20001
                           ,v_sql_errmsg);
END;

-- How to use this function in a select statement:
select line_wrap(my_string,
                 40,
                 2,
                 ' ')
  from my_table.

Some code I got from Martin Burbridge and modified:

CREATE OR REPLACE FUNCTION line_wrap(p_str_to_wrap             VARCHAR2
                                    ,p_max_linesize            PLS_INTEGER
                                    ,p_indent_spaces_each_line PLS_INTEGER DEFAULT 0
                                    ,p_separator               VARCHAR2 DEFAULT ' ') RETURN VARCHAR2 IS
  -- This function will insert chr(10)'s (newlines) at the separator 
  --  nearest the specified linesize.
  -- The separator will default to a space if none provided.
  -- The p_indent_spaces_each_line parameter allows each line of wrapped text to be
  --  indented x spaces if desired. The indent_spaces will default to 0 if none provided. 
  v_max_linesize            PLS_INTEGER := nvl(p_max_linesize
                                              ,80);
  v_indent_spaces_each_line PLS_INTEGER := nvl(p_indent_spaces_each_line
                                              ,0);
  v_separator               VARCHAR2(20) := nvl(p_separator
                                               ,' ');
  v_str_to_wrap             VARCHAR2(4000) := p_str_to_wrap || v_separator;
  v_line                    VARCHAR2(4000);
  v_position                PLS_INTEGER;
  v_wrapped_text            VARCHAR2(4000);
  v_sql_errmsg              VARCHAR2(4000);

BEGIN
  WHILE v_str_to_wrap IS NOT NULL
  LOOP
    v_line     := substr(v_str_to_wrap
                        ,1
                        ,v_max_linesize);
    v_position := instr(v_line
                       ,v_separator
                       ,-1);
    IF v_position = 0
    THEN
      v_position := v_max_linesize;
    END IF;

    v_line := substr(v_line
                    ,1
                    ,v_position);

    IF v_indent_spaces_each_line > 0
    THEN
      v_wrapped_text := v_wrapped_text || chr(10) || lpad(' '
                                                         ,v_indent_spaces_each_line
                                                         ,' ') || v_line;
    ELSE
      v_wrapped_text := v_wrapped_text || chr(10) || v_line;
    END IF;

    v_str_to_wrap := substr(v_str_to_wrap
                           ,v_position + 1);
  END LOOP;

  RETURN v_wrapped_text;

EXCEPTION
  WHEN OTHERS THEN
    v_sql_errmsg := 'Error in word_wrap: ' || SQLERRM;
    raise_application_error(-20001
                           ,v_sql_errmsg);
END;

-- How to use this function in a select statement:
select line_wrap(my_string,
                 40,
                 2,
                 ' ')
  from my_table.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文