4. 使用函数处理数据

本节使用的表

vendors

postgres=# \d vendors

                    Table "public.vendors"
    Column    |     Type      | Collation | Nullable | Default
--------------+---------------+-----------+----------+---------
 vend_id      | character(10) |           | not null |
 vend_name    | character(50) |           | not null |
 vend_address | character(50) |           |          |
 vend_city    | character(50) |           |          |
 vend_state   | character(5)  |           |          |
 vend_zip     | character(10) |           |          |
 vend_country | character(50) |           |          |
Indexes:
    "vendors_pkey" PRIMARY KEY, btree (vend_id)
Referenced by:
    TABLE "products" CONSTRAINT "fk_products_vendors" FOREIGN KEY (vend_id) REFERENCES vendors(vend_id)
SELECT
    *
FROM
    vendors;

 vend_id |  vend_name    | vend_address      | vend_city  | vend_state | vend_zip | vend_country
---------+---------------+-------------------+------------+------------+----------+------------------
 BRS01   | Bears R Us    | 123 Main Street   | Bear Town  | MI         | 44444    | USA
 BRE02   | Bear Emporium   | 500 Park Street | Anytown    | OH         | 44333    | USA
 DLL01   | Doll House Inc. | 555 High Street | Dollsville | CA         | 99999    | USA
 FRB01   | Furball Inc.    | 1000 5th Avenue | New York   | NY         | 11111    | USA
 FNG01   | Fun and Games   | 42 Galaxy Road  | London     |            | N16 6PS  | England
 JTS01   | Jouets et ours  | 1 Rue Amusement | Paris      |            | 45678    | France
(6 rows)

orders

postgres=# \d orders

                    Table "public.orders"
   2Column   |     Type      | Collation | Nullable | Default
------------+---------------+-----------+----------+---------
 order_num  | integer       |           | not null |
 order_date | date          |           | not null |
 cust_id    | character(10) |           | not null |
Indexes:
    "orders_pkey" PRIMARY KEY, btree (order_num)
Foreign-key constraints:
    "fk_orders_customers" FOREIGN KEY (cust_id) REFERENCES customers(cust_id)
Referenced by:
    TABLE "orderitems" CONSTRAINT "fk_orderitems_orders" FOREIGN KEY (order_num) REFERENCES orders(order_num)
SELECT
    *
FROM
    orders;

 order_num | order_date |  cust_id
-----------+------------+------------
     20005 | 2012-05-01 | 1000000001
     20006 | 2012-01-12 | 1000000003
     20007 | 2012-01-30 | 1000000004
     20008 | 2012-02-03 | 1000000005
     20009 | 2012-02-08 | 1000000001
(5 rows)

文本处理函数

常用的文本处理函数:

函数说明备注
LEFT()返回字符串左边的字符或使用子字符串函数
LENGTH()返回字符串的长度也使用DATALENGTH()或len()
LOWER()将字符串转换为小写
LTRIM()去掉字符串左边的空格
RIGHT()返回字符串右边的字符或使用子字符串函数
RTRIM()去掉字符串右边的空格
SOUNDEX()返回字符串的SOUNDEX值
UPPER()将字符串转换为大写

使用UPPER()函数:

SELECT
    vend_name,
    upper(vend_name) AS vend_name_upcase 
FROM
    vendors 
ORDER BY
    vend_name;

                     vend_name                      | vend_name_upcase
----------------------------------------------------+------------------
 Bear Emporium                                      | BEAR EMPORIUM
 Bears R Us                                         | BEARS R US
 Doll House Inc.                                    | DOLL HOUSE INC.
 Fun and Games                                      | FUN AND GAMES
 Furball Inc.                                       | FURBALL INC.
 Jouets et ours                                     | JOUETS ET OURS
(6 rows)

日期和时间处理函数

SELECT
    vend_name,
    upper(vend_name) AS vend_name_upcase 
FROM
    vendors 
ORDER BY
    vend_name;

 order_num | order_date
-----------+------------
     20005 | 2012-05-01
     20006 | 2012-01-12
     20007 | 2012-01-30
     20008 | 2012-02-03
     20009 | 2012-02-08
(5 rows)

数值处理函数

函数说明
ABS()返回一个数的绝对值
COS()返回一个角度的余弦
EXP()返回一个数的指数值
PI()返回圆周率
SIN()返回一个角度的正弦
SQRT()返回一个数的平方根
TAN()返回一个角度的正切
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值