PostgreSQL 常用函數

PostgreSQL 內置函數也稱為聚合函數,用於對字串或數字數據執行處理。

下麵是所有通用 PostgreSQL 內置函數的列表:

  • COUNT 函數:用於計算資料庫表中的行數。
  • MAX 函數:用於查詢某一特定列中最大值。
  • MIN 函數:用於查詢某一特定列中最小值。
  • AVG 函數:用於計算某一特定列中平均值。
  • SUM 函數:用於計算數字列所有值的總和。
  • ARRAY 函數:用於輸入值(包括null)添加到數組中。
  • Numeric 函數:完整列出一個 SQL 中所需的運算元的函數。
  • String 函數:完整列出一個 SQL 中所需的操作字元的函數。

數學函數

下麵是PostgreSQL中提供的數學函數列表,需要說明的是,這些函數中有許多都存在多種形式,區別只是參數類型不同。除非特別指明,任何特定形式的函數都返回和它的參數相同的數據類型。

函數 返回類型 描述 例子 結果
abs(x) 絕對值 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) 自然對數 ln(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 "π"常量 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

三角函數列表

函數 描述
acos(x) 反余弦
asin(x) 反正弦
atan(x) 反正切
atan2(x, y) 正切 y/x 的反函數
cos(x) 余弦
cot(x) 餘切
sin(x) 正弦
tan(x) 正切

字串函數和操作符

下麵是 PostgreSQL 中提供的字串操作符列表:

函數 返回類型 描述 例子 結果
string 丨丨 string text 字串連接 'Post' 丨丨 'greSQL' PostgreSQL
bit_length(string) int 字串裏二進位位的個數 bit_length('jose') 32
char_length(string) int 字串中的字元個數 char_length('jose') 4
convert(string using conversion_name) text 使用指定的轉換名字改變編碼。 convert('PostgreSQL' using iso_8859_1_to_utf8) 'PostgreSQL'
lower(string) text 把字串轉化為小寫 lower('TOM') tom
octet_length(string) int 字串中的位元組數 octet_length('jose') 4
overlay(string placing string from int [for int]) text 替換子字串 overlay('Txxxxas' placing 'hom' from 2 for 4) Thomas
position(substring in string) int 指定的子字串的位置 position('om' in 'Thomas') 3
substring(string [from int] [for int]) text 抽取子字串 substring('Thomas' from 2 for 3) hom
substring(string from pattern) text 抽取匹配 POSIX 正則運算式的子字串 substring('Thomas' from '…$') mas
substring(string from pattern for escape) text 抽取匹配SQL正則運算式的子字串 substring('Thomas' from '%#"o_a#"_' for '#') oma
trim([leading丨trailing 丨 both] [characters] from string) text 從字串string的開頭/結尾/兩邊/ 刪除只包含characters(默認是一個空白)的最長的字串 trim(both 'x' from 'xTomxx') Tom
upper(string) text 把字串轉化為大寫。 upper('tom') TOM
ascii(text) int 參數第一個字元的ASCII碼 ascii('x') 120
btrim(string text [, characters text]) text 從string開頭和結尾刪除只包含在characters裏(默認是空白)的字元的最長字串 btrim('xyxtrimyyx','xy') trim
chr(int) text 給出ASCII碼的字元 chr(65) A
convert(string text, [src_encoding name,] dest_encoding name) text 把字串轉換為dest_encoding convert( 'text_in_utf8', 'UTF8', 'LATIN1') 以ISO 8859-1編碼表示的text_in_utf8
initcap(text) text 把每個單詞的第一個子母轉為大寫,其他的保留小寫。單詞是一系列字母數字組成的字元,用非字母數字分隔。 initcap('hi thomas') Hi Thomas
length(string text) int string中字元的數目 length('jose') 4
lpad(string text, length int [, fill text]) text 通過填充字元fill(默認為空白),把string填充為長度length。 如果string已經比length長則將其截斷(在右邊)。 lpad('hi', 5, 'xy') xyxhi
ltrim(string text [, characters text]) text 從字串string的開頭刪除只包含characters(默認是一個空白)的最長的字串。 ltrim('zzzytrim','xyz') trim
md5(string text) text 計算給出string的MD5散列,以十六進制返回結果。 md5('abc')
repeat(string text, number int) text 重複string number次。 repeat('Pg', 4) PgPgPgPg
replace(string text, from text, to text) text 把字串string裏出現地所有子字串from替換成子字串to。 replace('abcdefabcdef', 'cd', 'XX') abXXefabXXef
rpad(string text, length int [, fill text]) text 通過填充字元fill(默認為空白),把string填充為長度length。如果string已經比length長則將其截斷。 rpad('hi', 5, 'xy') hixyx
rtrim(string text [, character text]) text 從字串string的結尾刪除只包含character(默認是個空白)的最長的字 rtrim('trimxxxx','x') trim
split_part(string text, delimiter text, field int) text 根據delimiter分隔string返回生成的第field個子字串(1 Base)。 split_part('abc~@~def~@~ghi', '~@~', 2) def
strpos(string, substring) text 聲明的子字串的位置。 strpos('high','ig') 2
substr(string, from [, count]) text 抽取子字串。 substr('alphabet', 3, 2) ph
to_ascii(text [, encoding]) text 把text從其他編碼轉換為ASCII。 to_ascii('Karel') Karel
to_hex(number int/bigint) text 把number轉換成其對應地十六進製錶現形式。 to_hex(9223372036854775807) 7fffffffffffffff
translate(string text, from text, to text) text 把在string中包含的任何匹配from中的字元的字元轉化為對應的在to中的字元。 translate('12345', '14', 'ax') a23x5

類型轉換相關函數

函數 返回類型 描述 實例
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://blog.csdn.net/sun5769675/article/details/50628979