3

如何在char中保留空格?我创建了 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              |

我希望像甲骨文一样爆炸

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

有什么选择可以尝试吗?

4

1 回答 1

0

没有办法改变这种行为。

还要注意以下几点:

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 个字符,请使用检查约束或使用空格填充值的触发器。

于 2021-01-14T07:54:25.357 回答