欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年05月04日 09:45:01   投稿:junjie  
這篇文章主要介紹了PostgreSQL教程(五):函數(shù)和操作符詳解(1),本文講解了邏輯操作符、比較操作符、數(shù)學(xué)函數(shù)和操作符、三角函數(shù)列表、字符串函數(shù)和操作符等內(nèi)容,需要的朋友可以參考下

一、邏輯操作符:

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

二、比較操作符:

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

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

三、 數(shù)學(xué)函數(shù)和操作符:

    下面是PostgreSQL中提供的數(shù)學(xué)操作符列表:

按位操作符只能用于整數(shù)類型,而其它的操作符可以用于全部數(shù)值數(shù)據(jù)類型。按位操作符還可以用于位串類型bit和bit varying,

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

三角函數(shù)列表:

四、字符串函數(shù)和操作符:

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

 

函數(shù) 返回類型 描述 例子 結(jié)果
string || string text 字串連接 'Post' || 'greSQL' PostgreSQL
bit_length(string) int 字串里二進(jìn)制位的個數(shù) bit_length('jose') 32
char_length(string) int 字串中的字符個數(shù) char_length('jose') 4
convert(string using conversion_name) text 使用指定的轉(zhuǎn)換名字改變編碼。 convert('PostgreSQL' using iso_8859_1_to_utf8) 'PostgreSQL'
lower(string) text 把字串轉(zhuǎn)化為小寫 lower('TOM') tom
octet_length(string) int 字串中的字節(jié)數(shù) 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 正則表達(dá)式的子字串 substring('Thomas' from '...$') mas
substring(string from pattern for escape) text 抽取匹配SQL正則表達(dá)式的子字串 substring('Thomas' from '%#"o_a#"_' for '#') oma
trim([leading | trailing | both] [characters] from string) text 從字串string的開頭/結(jié)尾/兩邊/ 刪除只包含characters(缺省是一個空白)的最長的字串 trim(both 'x' from 'xTomxx') Tom
upper(string) text 把字串轉(zhuǎn)化為大寫。 upper('tom') TOM
ascii(text) int 參數(shù)第一個字符的ASCII碼 ascii('x') 120
btrim(string text [, characters text]) text 從string開頭和結(jié)尾刪除只包含在characters里(缺省是空白)的字符的最長字串 btrim('xyxtrimyyx','xy') trim
chr(int) text 給出ASCII碼的字符 chr(65) A
convert(string text, [src_encoding name,] dest_encoding name) text 把字串轉(zhuǎn)換為dest_encoding convert( 'text_in_utf8', 'UTF8', 'LATIN1') 以ISO 8859-1編碼表示的text_in_utf8
initcap(text) text 把每個單詞的第一個子母轉(zhuǎn)為大寫,其它的保留小寫。單詞是一系列字母數(shù)字組成的字符,用非字母數(shù)字分隔。 initcap('hi thomas') Hi Thomas
length(string text) int string中字符的數(shù)目 length('jose') 4
lpad(string text, length int [, fill text]) text 通過填充字符fill(缺省時為空白),把string填充為長度length。 如果string已經(jīng)比length長則將其截斷(在右邊)。 lpad('hi', 5, 'xy') xyxhi
ltrim(string text [, characters text]) text 從字串string的開頭刪除只包含characters(缺省是一個空白)的最長的字串。 ltrim('zzzytrim','xyz') trim
md5(string text) text 計算給出string的MD5散列,以十六進(jìn)制返回結(jié)果。 md5('abc')  
repeat(string text, number int) text 重復(fù)string number次。 repeat('Pg', 4) PgPgPgPg
replace(string text, from text, to text) text 把字串string里出現(xiàn)地所有子字串from替換成子字串to。 replace('abcdefabcdef', 'cd', 'XX') abXXefabXXef
rpad(string text, length int [, fill text]) text 通過填充字符fill(缺省時為空白),把string填充為長度length。如果string已經(jīng)比length長則將其截斷。 rpad('hi', 5, 'xy') hixyx
rtrim(string text [, character text]) text 從字串string的結(jié)尾刪除只包含character(缺省是個空白)的最長的字 rtrim('trimxxxx','x') trim
split_part(string text, delimiter text, field int) text 根據(jù)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從其它編碼轉(zhuǎn)換為ASCII。 to_ascii('Karel') Karel
to_hex(number int/bigint) text 把number轉(zhuǎn)換成其對應(yīng)地十六進(jìn)制表現(xiàn)形式。 to_hex(9223372036854775807) 7fffffffffffffff
translate(string text, from text, to text) text 把在string中包含的任何匹配from中的字符的字符轉(zhuǎn)化為對應(yīng)的在to中的字符。 translate('12345', '14', 'ax') a23x5

五、位串函數(shù)和操作符:

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

除了以上列表中提及的操作符之外,位串還可以使用字符串函數(shù):length, bit_length, octet_length, position, substring。此外,我們還可以在整數(shù)和bit之間來回轉(zhuǎn)換,如:

復(fù)制代碼 代碼如下:

    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)

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

    該博客中提供的所有信息均源自PostgreSQL官方文檔,編寫該篇博客的主要目的是便于今后的查閱,特此聲明。

相關(guān)文章

最新評論