PostgreSQL學習手冊(函式和操作符<一>)

greenteazsh發表於2013-04-26

一、邏輯操作符:

    常用的邏輯操作符有:ANDORNOT。其語義與其它程式語言中的邏輯操作符完全相同。

二、比較操作符:

    下面是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中提供的數學操作符列表:

操作符 描述 例子 結果
+ 2 + 3 5
- 2 - 3 -1
* 2 * 3 6
/ 4 / 2 2
% 5 % 4 1
^ 2.0 ^ 3.0 8
|/ 平方根 |/ 25.0 5
||/ 立方根 ||/ 27.0 3
! 階乘 5 ! 120
!! 階乘 !! 5 120
@ 絕對值 @ -5.0 5
& 按位AND 91 & 15 11
| 按位OR 32 | 3 35
# 按位XOR 17 # 5 20
~ 按位NOT ~1 -2
<< 按位左移 1 << 4 16
>> 按位右移 8 >> 2 2

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

    下面是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


五、位串函式和操作符:

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

操作符 描述 例子 結果
|| 連線 B'10001' || B'011' 10001011
& 按位AND B'10001' & B'01101' 00001
| 按位OR B'10001' | B'01101' 11101
# 按位XOR B'10001' # B'01101' 11100
~ 按位NOT ~ B'10001' 01110
<< 按位左移 B'10001' << 3 01000
>> 按位右移 B'10001' >> 2 00100

    除了以上列表中提及的操作符之外,位串還可以使用字串函式: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),因此只會轉換成整數的最低位。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9521459/viewspace-759334/,如需轉載,請註明出處,否則將追究法律責任。

相關文章