【问题标题】:in postgresql, how to preserve spaces in char在 postgresql 中,如何在 char 中保留空格
【发布时间】:2021-04-19 04:48:56
【问题描述】:

如何在字符中保留空格? 我创建了 2 个表,

create table test_1 (a int, b char(10)) ;
create table test_2 (a int, b varchar(255));

并在 test_1 中插入一行

insert into test_1 values (1 ,'     ');


insert into test_2 select * from test_1;
select a, length(b) from test_2;

然后它返回

| a        | length(b)      |
| -------- | -------------- |
| 1        | 0              |

我预计会像 oracle 一样崩溃

| a        | length(b)      |
| -------- | -------------- |
| 1        | 10             |

有什么办法可以试试吗?

【问题讨论】:

标签: postgresql spaces trailing-whitespace


【解决方案1】:

没有办法改变这种行为。

还要注意以下几点:

CREATE TABLE test_1 (a int, b char(10));

INSERT INTO test_1 VALUES (1, 'x');

SELECT length(b) FROM test_1;

 length 
--------
      1
(1 row)

SELECT 'y' || b FROM test_1;

 ?column? 
----------
 yx
(1 row)

所有这些都按照 SQL 标准的要求工作。

帮自己一个忙,永远不要使用char。如果您需要该值始终包含 10 个字符,请使用检查约束或用空格填充值的触发器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 2016-11-03
    • 2013-02-03
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多