定义COLLATE变量

最近更新时间: 2025-02-18 16:02:00

按unicode值对比大小。

  • 复制
    复制成功
postgres=# CREATE OR REPLACE FUNCTION f25() RETURNS VOID AS
postgres-# $$
postgres$# DECLARE    
postgres$#     v_txt1 TEXT COLLATE "C" := '严';     
postgres$#     v_txt2 TEXT COLLATE "C" := '丰';     
postgres$# BEGIN
postgres$#     IF v_txt1 > v_txt2 THEN
postgres$#         RAISE NOTICE ' % -> % ',v_txt1,v_txt2;
postgres$#     ELSE
postgres$#         RAISE NOTICE ' % -> % ',v_txt2,v_txt1;
postgres$#     END IF; 
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f25();  
NOTICE:   丰 -> 严 
 f25 
-----
(1 row)
postgres=# select '严'::bytea;
  bytea   
----------
 \xe4b8a5
(1 row)
postgres=# select '丰'::bytea;
  bytea   
----------
 \xe4b8b0
(1 row)
#按汉字的拼音对比大小
postgres=# CREATE OR REPLACE FUNCTION f25() RETURNS VOID AS
postgres-# $$
postgres$# DECLARE    
postgres$#     v_txt1 TEXT COLLATE "zh_CN.utf8" := '严';     
postgres$#     v_txt2 TEXT COLLATE "zh_CN.utf8" := '丰';     
postgres$# BEGIN
postgres$#     IF v_txt1 > v_txt2 THEN
postgres$#         RAISE NOTICE ' % -> % ',v_txt1,v_txt2;
postgres$#     ELSE
postgres$#         RAISE NOTICE ' % -> % ',v_txt2,v_txt1;
postgres$#     END IF; 
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f25();  
NOTICE:   严 -> 丰 
 f25 
----- 
(1 row)