PostgreSQL支持3种字符串类型,分别是character varying(n)、character(n)和text,character varying(n)能够写成varchar(n),character(n)能够写成char(n),最大长度1GB,text最大长度无限制。n是实际字符数量。函数
大部分状况下,推荐使用text和varchar(n)。post
PostgreSQL支持一种二进制类型bytea。编码
二进制数据有图片(JPG、PNG等)、音乐(MP三、WMA等)格式文件,和字符串文件区别在于文本文件须要复合文件编码和储存可见字符,二进制文件没有相似限制,字符串适合储存文本文件。code
---字符串练习 postgres=# create table testtext(testtext char(2),testvarchar varchar(3), testchar text); CREATE TABLE postgres=# postgres=# postgres=# insert into testtext values('1','111','111'),('0','000','011777'); INSERT 0 2 postgres=# select * from testtext; testtext | testvarchar | testchar ----------+-------------+---------- 1 | 111 | 111 0 | 000 | 011777 (2 行记录) postgres=#
插入长度超过预约值是,提示错误信息。图片
下面演示部分常见字符串函数。字符串
---字符串拼接 postgres=# select 'N'||'O'; ?column? ---------- NO (1 行记录) postgres=# select 'Hello'||' '||'World'; ?column? ------------- Hello World (1 行记录) postgres=#
根据以上代码字符串能够拼接2个以上字符串。it
---1Byte=8Bite postgres=# select bit_length('A'); bit_length ------------ 8 (1 行记录) postgres=#
检查字符串byte长度。table
postgres=# select char_length('A'); char_length ------------- 1 (1 行记录) postgres=# select char_length('Hello world.'); char_length ------------- 12 (1 行记录) postgres=#
字符串转换为小写。test
postgres=# select lower('Hello'); lower ------- hello (1 行记录) postgres=# select lower('HI'); lower ------- hi (1 行记录) postgres=#
字符串转换为大写。select
postgres=# select upper('Hello'); upper ------- HELLO (1 行记录) postgres=# select upper('Hi'); upper ------- HI (1 行记录) postgres=#