PostgreSQL教程(五):函數和操作符詳解(1),postgresql操作符

來源:互聯網
上載者:User

PostgreSQL教程(五):函數和操作符詳解(1),postgresql操作符

一、邏輯操作符:

    常用的邏輯操作符有:AND、OR和NOT。其語義與其它程式設計語言中的邏輯操作符完全相同。

二、比較操作符:

    下面是PostgreSQL中提供的比較操作符列表:

比較操作符可以用於所有可以比較的資料類型。所有比較操作符都是雙目操作符,且返回boolean類型。除了比較操作符以外,我們還可以使用BETWEEN語句,如:
    a BETWEEN x AND y 等效於 a >= x AND a <= y   
    a NOT BETWEEN x AND y 等效於 a < x OR a > y

三、 數學函數和操作符:

    下面是PostgreSQL中提供的數學操作符列表:

按位操作符只能用於整數類型,而其它的操作符可以用於全部數值資料類型。按位操作符還可以用於位串類型bit和bit varying,

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

三角函數列表:

四、字串函數和操作符:

    下面是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 Regex的子字串 substring('Thomas' from '...$') mas
substring(string from pattern for escape) text 抽取匹配SQLRegex的子字串 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

五、位串函數和操作符:

    對於類型bit和bit varying,除了常用的比較操作符之外,還可以使用以下列表中由PostgreSQL提供的位串函數和操作符,其中&、|和#的位串運算元必須等長。在移位的時候,保留原始的位串的的長度。

除了以上列表中提及的操作符之外,位串還可以使用字串函數:length, bit_length, octet_length, position, substring。此外,我們還可以在整數和bit之間來迴轉換,如:
複製代碼 代碼如下:
    MyTest=# SELECT 44::bit(10);
        bit
    ------------
     0000101100
    (1 row)
    MyTest=# SELECT 44::bit(3);
     bit
    -----
     100
    (1 row)
    MyTest=# SELECT cast(-44 as bit(12));
         bit
    --------------
     111111010100
    (1 row)
    MyTest=# SELECT '1110'::bit(4)::integer;
     int4
    ------
       14
    (1 row)

    注意:如果只是轉換為"bit",意思是轉換成bit(1),因此只會轉換成整數的最低位。

    該部落格中提供的所有資訊均源自PostgreSQL官方文檔,編寫該篇部落格的主要目的是便於今後的查閱,特此聲明。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.