

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# 字符类型的示例
<a name="r_Examples_with_character_types"></a>

## CREATE TABLE 语句
<a name="r_Examples_with_character_types-create-table-statement"></a>

以下 CREATE TABLE 语句演示 VARCHAR 和 CHAR 数据类型的使用：

```
create table address(
address_id integer,
address1 varchar(100),
address2 varchar(50),
district varchar(20),
city_name char(20),
state char(2),
postal_code char(5)
);
```

下列示例使用此表。

## 可变长度字符串中的尾部空格
<a name="r_Examples_with_character_types-trailing-blanks-in-variable-length-character-strings"></a>

由于 ADDRESS1 是 VARCHAR 列，插入的第二个地址中的尾部空格始终无语义意义。换言之，插入的这两个地址*匹配*。

```
insert into address(address1) values('9516 Magnolia Boulevard');

insert into address(address1) values('9516 Magnolia Boulevard  ');
```

```
select count(*) from address
where address1='9516 Magnolia Boulevard';

count
-------
2
(1 row)
```

如果 ADDRESS1 列是 CHAR 列并且插入了相同的值，则 COUNT(\*) 查询会将字符串识别为相同并返回 `2`。

## LENGTH 函数的结果
<a name="r_Examples_with_character_types-results-of-the-length-function"></a>

LENGTH 函数识别 VARCHAR 列中的尾部空格：

```
select length(address1) from address;

length
--------
23
25
(2 rows)
```

CITY\_NAME 列（是 CHAR 列）中的 `Augusta` 的值，不管输入字符串有任何尾部空格，始终返回 7 个字符的长度。

## 超出列长度的值
<a name="r_Examples_with_character_types-values-that-exceed-the-length-of-the-column"></a>

未截断字符串来匹配列的声明宽度：

```
insert into address(city_name) values('City of South San Francisco');
ERROR: value too long for type character(20)
```

解决此问题的一个办法是将此值强制转换为列的大小：

```
insert into address(city_name)
values('City of South San Francisco'::char(20));
```

在此示例中，字符串 (`City of South San Fr`) 的前 20 个字符将加载到列中。