在Postgres psql shell中,是否可以只显示一个大文本字段的第一行?

时间:2016-08-13 01:25:38

标签: sql postgresql shell

我将一大块文本保存到Postgres表的文本字段中。

我想看看我的psql shell上显示的文本字段的前20个左右字符。

我目前正在使用psql运行:

select * from tablename;

并且它没有在终端中显示任何内容,因为它太大了。

数据肯定会保存,因为我可以在线显示它们。

正如您在描述表格时所看到的,我正在使用的“datatext”字段是“文本”类型。

 \d+ mytablename;
                                                       Table "public.mytablename"
  Column  |           Type           |                         Modifiers                          | Storage  | Stats target | Description
----------+--------------------------+------------------------------------------------------------+----------+--------------+-------------
 id       | integer                  | not null default nextval('mytablename_id_seq'::regclass) | plain    |              |
 when     | timestamp with time zone | not null                                                   | plain    |              |
 datatext | text                     | not null                                                   | extended |              |
Indexes:
    "mytablename_pkey" PRIMARY KEY, btree (id)

1 个答案:

答案 0 :(得分:1)

如果您只想在字段中显示文本的前20个字符,则可以执行以下操作:

SELECT LEFT(datatext,20) AS First_20_Chars FROM tablename LIMIT 1;

您可以使用最后的LIMIT语句控制返回的记录数。