PostgreSQL 常用函式
PostgreSQL內置函式也稱為聚合函式,用于對字串或數字資料執行處理,
下面是所有通用PostgreSQL內置函式的串列:
- COUNT 函式:用于計算資料庫表中的行數,
- MAX 函式:用于查詢某一特定列中最大值,
- MIN 函式:用于查詢某一特定列中最小值,
- AVG 函式:用于計算某一特定列中平均值,
- SUM 函式:用于計算數字列所有值的總和,
- ARRAY 函式:用于輸入值(包括null)添加到陣列中,
- Numeric 函式:完整列出一個SQL中所需的運算元的函式,
- String 函式:完整列出一個SQL中所需的操作字符的函式,
數字函式
需要說明的是,這些函式中有許多都存在多種形式,區別只是引數型別不同,除非特別指明,任何特定形式的函式都回傳和他的引數相同的資料型別,
| 函式 | 回傳型別 | 描述 | 例子 | 結果 |
| abs() | 絕對值 | abs(-17.4) | 17.4 | |
| cbrt(double) | 立方根 | cbrt(27.0) | 3 | |
| ceil(double/numeric) | 不小于引數的最小整數 | ceil(-42.8) | -42 | |
| degrees(double) | 把弧度轉為角度 | degrees(0.5) | 28.6478897565412 | |
| exp(double/numeric) | 自然指數 | exp(1.0) | 2.71828182845905 | |
| floor(double/numeric) | 不大于引數的最大整數 | floor(-42.8) | -43 | |
| ln(double/numeric) | 自然對數 | In(2.0) | 0.693147180559945 | |
| log(double/numeric) | 10為底的對數 | log(100,0) | 2 | |
| log(b numeric,x numeric) | numeric | 指定底數的對數 | log(2.0,64.0) | 6.0000000000 |
| mod(y,x) | 取余數 | mod(9,4) | 1 | |
| pi() | double | "TT"常量 | pi() | 3.14159265358979 |
| power(a double,b double) | double | 求a的b次冪 | power(9.0,3.0) | 729 |
| Power(a numeric,b numeric) | numeric | 求a的b次冪 | power(9.0,3.0) | 729 |
| radians(double) | double | 把角度轉為弧度 | radians(45.0) | 0.785398163397448 |
| random() | double | 0.0到1.0之間的亂數值 | random() | |
| round(double/numeric) | 圓整為最接近的整數 | round(42.4) | 42 | |
| round(v numeric,s int) | numeric | 圓整為s位小數數字 | round(42.438,2) | 42.44 |
| sign(double/numeric) | 引數的符號(-1,0,+1) | sign(-8.4) | -1 | |
| sqrt(double/numeric) | 平方根 | sqrt(2.0) | 1.4142135623731 | |
| trunc(double/numeric) | 截斷(向零靠近) | trunc(42.8) | 42 | |
| trunc(v numeric,s int) | numeric | 截斷為s小數位置的數字 | trunc(42.438,2) | 42.43 |
字串函式和運算子
下面是PostgreSQL中提供的字串運算子串列
| 函式 | 回傳型別 | 說明 | 例子 | 結果 |
| string || string | text | 字串連接 | 'Post' || 'greSQL' | PostgreSQL |
| bit_length(string) | int | 字串里二進制位的個數 | bit_length('netstars') | 64 |
| char_length(string) | int | 字串中的字符個數 | char_length('netstars') | 8 |
| lower(string) | text | 使用指定的轉換名字改變編碼, | lower('NETSTARS') | netstars |
| octet_length(string) | int | 字串中的位元組數 | octet_length('netstars') | 8 |
| overlay(string placing string from int for int) | text | 替換子字串 | overlay('XXXstars' placing 'net' from 1 for 3) | netstars |
| position(substring in 'string') | int | 指定的子字串的位置 | position('st' in 'netstars') | 4 |
| substring(string from int for int) | text | 抽取子字串 | substring('netstars' from 4 for 8) | stars |
| upper(string) | text | 把字串轉化為大寫, | upper('netstars') | NETSTARS |
| ascii(text) | int | 引數第一個字符的ASCII碼 | ascii('n') | 110 |
| chr(int) | text | 給出ASCII碼的字符 | chr(int) | A |
| select initcap(string) | text | 把每個單詞的第一個子母轉為大寫,其它的保留小寫,單詞是一系列字母數字組成的字符,用非字母數字分隔, | select initcap('net stars') | |
| length(string text) | int | string中字符的數目 | length('netstars core') | 13 |
| repeat(string text, number int) | text | 重復string number次 | repeat('net', 3) | netnetnet |
型別轉換相關函式
| 函式 | 回傳型別 | 描述 | 實體 |
|---|---|---|---|
| to_char(timestamp, text) | text | 將時間戳轉換為字串 | to_char(current_timestamp, 'HH12:MI:SS') |
| to_char(interval, text) | text | 將時間間隔轉換為字串 | to_char(interval '15h 2m 12s', 'HH24:MI:SS') |
| to_char(int, text) | text | 整型轉換為字串 | to_char(125, '999') |
| to_char(double precision, text) | text | 雙精度轉換為字串 | to_char(125.8::real, '999D9') |
| to_char(numeric, text) | text | 數字轉換為字串 | to_char(-125.8, '999D99S') |
| to_date(text, text) | date | 字串轉換為日期 | to_date('05 Dec 2000', 'DD Mon YYYY') |
| to_number(text, text) | numeric | 轉換字串為數字 | to_number('12,454.8-', '99G999D9S') |
| to_timestamp(text, text) | timestamp | 轉換為指定的時間格式 time zone convert string to time stamp | to_timestamp('05 Dec 2000', 'DD Mon YYYY') |
| to_timestamp(double precision) | timestamp | 把UNIX紀元轉換成時間戳 | to_timestamp(1284352323) |
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/40072.html
標籤:NoSQL
上一篇:Redis AOF 持久化詳解
