1.string || string 字符串连接
hxf=# select * from huangxifeng ;
first_name | last_name
------------+-----------
huang | xifeng
(1 row)
hxf=# select first_name||'.'||last_name as fullname from huangxifeng ;
fullname
--------------
huang.xifeng
(1 row)
两个字段连接成名字,中间连接"."。
hxf=# select last_name||'.'||first_name as fullname from huangxifeng ;
fullname
--------------
xifeng.huang
(1 row)
两个字段连接成名字,中间连接"."。字段先后可随意调整。
2.bit_length
(string) 字符串里二进制位的个数
hxf=# select first_name,bit_length(first_name) from huangxifeng ;
first_name | bit_length
------------+------------
huang | 40
(1 row)
一个字符8位,共5个字符,所以40位长。
hxf=# select last_name,bit_length(last_name) from huangxifeng ;
last_name | bit_length
-----------+------------
xifeng | 48
(1 row)
一个字符8位,共6个字符,所以48位长。
3.char_length
(string) 或 character_length
(string) 字符串中的字符个数
hxf=# select first_name,char_length(first_name) from huangxifeng ;
first_name | char_length
------------+-------------
huang | 5
(1 row)
共5个字符
hxf=# select last_name,char_length(last_name) from huangxifeng ;
last_name | char_length
-----------+-------------
xifeng | 6
(1 row)
共6个字符
hxf=# select first_name,char_length(first_name) from huangxifeng2;
first_name | char_length
------------+-------------
黄 | 1
(1 row)
中文1个字符
hxf=# select last_name,char_length(last_name) from huangxifeng2;
last_name | char_length
-----------+-------------
锡峰 | 2
(1 row)
中文2个字符
4.octet_length
(string) 字符串中的字节数
hxf=# select first_name,octet_length(first_name) from huangxifeng;
first_name | octet_length
------------+--------------
huang | 5
(1 row)
英文5个字符就是5字节
hxf=# select last_name,octet_length(last_name) from huangxifeng;
last_name | octet_length
-----------+--------------
xifeng | 6
(1 row)
英文6个字符就是6字节
hxf=# select first_name,octet_length(first_name) from huangxifeng2;
first_name | octet_length
------------+--------------
黄 | 3
(1 row)
1个中文字符是3字节(数据库环境是utf8编码)
hxf=# select last_name,octet_length(last_name) from huangxifeng2;
last_name | octet_length
-----------+--------------
锡峰 | 6
(1 row)
2个中文字符就是6个字节了(数据库环境是utf8编码)
5.overlay
(string placing string from int [for int]) 替换子字符串
hxf=# select overlay(last_name placing 'GG' from 7 for 2) from huangxifeng;
overlay
----------
xifengGG
(1 row)
'xifeng'一共是6位,从第7位开始的2位加GG。
6.position
(substring in string) 指定的子字符串的位置
hxf=# select last_name,position('x' in last_name) from huangxifeng;
last_name | position
-----------+----------
xifeng | 1
(1 row)
'x'在last_name中第1位
hxf=# select last_name,position('i' in last_name) from huangxifeng;
last_name | position
-----------+----------
xifeng | 2
(1 row)
'i'在last_name中第2位
hxf=# select last_name,position('f' in last_name) from huangxifeng;
last_name | position
-----------+----------
xifeng | 3
(1 row)
'f'在last_name中第3位
7.substring
(string [from int] [for int]) 抽取子字符串
hxf=# select last_name,substring(last_name from 1 for 2) from huangxifeng;
last_name | substring
-----------+-----------
xifeng | xi
(1 row)
提取第1位开始的2个字符
hxf=# select last_name,substring(last_name from 3 for 4) from huangxifeng;
last_name | substring
-----------+-----------
xifeng | feng
(1 row)
提取第3位开始的4个字符
8.substring
(string from pattern)抽取匹配 POSIX 正则表达式的子字符串。
hxf=# select last_name,substring(last_name from '^..') from huangxifeng;
last_name | substring
-----------+-----------
xifeng | xi
00xifeng | 00
11xifeng | 11
22xifeng | 22
AAxifeng | AA
BBxifeng | BB
CCxifeng | CC
(7 rows)
抽取开始的任意两个字符
hxf=# select last_name,substring(last_name from '......$') from huangxifeng;
last_name | substring
-----------+-----------
xifeng | xifeng
00xifeng | xifeng
11xifeng | xifeng
22xifeng | xifeng
AAxifeng | xifeng
BBxifeng | xifeng
CCxifeng | xifeng
(7 rows)
抽取结尾的任意6个字符。
9.lower
(string)把字符串转化为小写,upper
(string)把字符串转化为大写
hxf=# select last_name,lower(last_name),upper(last_name) from huangxifeng;
last_name | lower | upper
-----------+----------+----------
xifeng | xifeng | XIFENG
00xifeng | 00xifeng | 00XIFENG
11xifeng | 11xifeng | 11XIFENG
22xifeng | 22xifeng | 22XIFENG
AAxifeng | aaxifeng | AAXIFENG
BBxifeng | bbxifeng | BBXIFENG
CCxifeng | ccxifeng | CCXIFENG
(7 rows)
10.trim
([leading
| trailing | both] [characters] from string)从字符串 string 的开头/结尾/两边删除只包含 characters 中字符(缺省是一个空白)的最长的字符串
hxf=# select char_length(' huang '),trim(' huang '),length(trim(' huang ')) ;
char_length | btrim | length
-------------+-------+--------
7 | huang | 5
(1 row)
' huang '前后有一空格共7位,trim默认去掉前后空格,去掉后共5位。
hxf=# select last_name,trim('00' from last_name) from huangxifeng;
last_name | btrim
-----------+----------
xifeng | xifeng
00xifeng | xifeng
(2 rows)
第二行前导的00不见了。
hxf=# select last_name,trim('xifeng' from last_name) from huangxifeng;
last_name | btrim
-----------+----------
xifeng |
00xifeng | 00
11xifeng | 11
22xifeng | 22
AAxifeng | AA
BBxifeng | BB
CCxifeng | CC
DxifengD | DxifengD
(8 rows)
所有后面含有'xifeng'的单词不见了,第8行没去掉,因为不在前后。
11.ascii 参数第一个字符的 ASCII 码,chr给出 ASCII 码的字符
例:
hxf=# select ascii('a');
ascii
-------
97
(1 row)
hxf=# select ascii('A');
ascii
-------
65
(1 row)
hxf=# select chr(97);
chr
-----
a
(1 row)
hxf=# select chr(65);
chr
-----
A
(1 row)
12.
阅读(2657) | 评论(0) | 转发(0) |