數據庫常用函數

1、concat(拼接字段)


postgres=# select concat(id,name,remark) from t_kenyon;
   concat    
-------------
 1testkenyon
 2justchina
 3iamlovingU
 4test
 5adele

2、concat_ws(拼接字段帶拼接符)


postgres=# select concat_ws('_',id,name,remark) from t_kenyon;
   concat_ws   
---------------
 1_test_kenyon
 2_just_china
 3_iam_lovingU
 4_test
 5_adele

3、split_apart(分割字段)

postgres=# select split_part('abc~@~def~@~ghi','~@~', 2);
 split_part 
------------
 def
(1 row)
 
postgres=# select split_part('now|year|month','|',3);
 split_part 
------------
 month
(1 row)

4、cast(類型轉換)

cast(aa as varchar)

5、coalesce(返回參數中的第一個非null的值,即將null賦值成某個定義字符)

select case when coalesce(name,'') = '' then '姓名爲空' else name end from student;

6、case when(條件判斷)

SELECT a,
       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
       END
    FROM test;

 a | case
---+-------
 1 | one
 2 | two
 3 | other
--------------------- 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章