Każdy system bazodanowy dostarcza podstawowe funkcje wbudowane umożliwiające dokonywanie typowych akcji na przechowywanych danych. Niestety w zależności od typu bazy danych i jej producenta dostarczane zestawy funkcji różnią się między sobą. Poniżej przedstawiono podstawowe funkcje dostępne w PstgreSQL.
Funkcje tekstowe
Poniższa tabelka zawiera podstawowe funkcje tekstowe dostępne w PostgreSQL.
Funkcja | Typ | Opis | Przykład | Wynik |
---|---|---|---|---|
string || string | text | Złączenie ciągów | Post' || 'greSQL' | PostgreSQL |
bit_length(string) | integer | Liczba bitów w ciągu | bit_length('dom') | 24 |
char_length(string) or character_length(string) | integer | Liczba znaków w ciągu | char_length('dom') | 3 |
convert(string using conversion_name) | text | Zmiana kodowania tekstu | convert('PostgreSQL' using iso_8859_1_to_utf_8) | PostgreSQL w kodwaniu UTF-8 |
lower(string) | text | Zamiana znaków na małe litery | lower('TOMEK') | tomek |
octet_length(string) | integer | Liczba bajtów w ciągu | octet_length('dom') | 3 |
overlay(string placing string from integer [for integer]) | text | Zamiana fragmentu tekstu | overlay('Txxxk' placing 'ome' from 2 for 3) | Tomek |
position(substring in string) | integer | Pozycja szukanego tekstu | position('om' in 'Tomek') | 2 |
substring(string [from integer] [for integer]) | text | Zwraca fragment tekstu | substring('Tomek' from 2 for 3) | ome |
substring(string from pattern) | text | Zwraca fragment tekstu | substring('Tomek' from '...$') | ek |
substring(string from pattern for escape) | text | Zwraca fragment tekstu | substring('Tomek' from '%#"o_e#"_' for '#') | ome |
trim([leading | trailing | both] [characters] from string) | text | Usuwa zbędne znaki (domyślnie spacje) | trim(both 'x' from 'xTomxx') | Tom |
upper(string) | text | Zamiana znaków na duże litery | upper('tomek') | TOMEK |
Funkcje matematyczne
Poniższa tabela zawiera operatory matematyczne dostępne w PostgreSQL.
Operator | Opis | Przykład | Wynik |
---|---|---|---|
+ | suma | 2 + 3 | 5 |
- | różnica | 2-3 | -1 |
* | iloczyn | 2 * 3 | 6 |
/ | iloraz | 4/2 | 2 |
% | reszta z dzielenia | 5 % 4 | 1 |
^ | potęga | 2.0 ^ 3.0 | 8 |
|/ | pierwiastek | |/ 25.0 | 5 |
||/ | pierwiastek 3st | ||/ 27.0 | 3 |
! | silnia | 5 ! | 120 |
!! | silnia | !! 5 | 120 |
@ | wartość bezwzględna | @ -5.0 | 5 |
& | AND | 91 & 15 | 11 |
| | OR | 32 | 3 | 35 |
# | XOR | 17 # 5 | 20 |
~ | NOT | ~1 | -2 |
<< | przesunięcie bitowe w lewo | 1 << 4 | 16 |
>> | przesunięcie bitowe w prawo | 8 >> 2 | 2 |
Funkcje matematyczne wbudowane w PostgreSQL.
Funkcja | Typ | Opis | Przykad | Wynik |
---|---|---|---|---|
abs(x) | - | wartość bezwzględna | abs(-17.4) | 17,4 |
cbrt(dp) | dp | pierwiastek 3 st | cbrt(27.0) | 3 |
ceil(dp or numeric) | - | część całkowita | ceil(-42.8) | -42 |
ceiling(dp or numeric) | - | część cakowita | ceiling(-95.3) | -95 |
degrees(dp) | dp | zamiana rad na st. | degrees(0.5) | 28,65 |
exp(dp or numeric) | - | exponential | exp(1.0) | 2,72 |
floor(dp or numeric) | - | część całkowita | floor(-42.8) | -43 |
ln(dp or numeric) | - | logarytm naturalny | ln(2.0) | 0,69 |
log(dp or numeric) | - | logarytm dziesiętny | log(100.0) | 2 |
log(b numeric, x numeric) | numeric | log przy podstawie b | log(2.0, 64.0) | 6 |
mod(y, x) | - | reszta z dzielenia | mod(9,4) | 1 |
pi() | dp | liczba PI | pi() | 3,14 |
power(a dp, b dp) | dp | a raised to the power of b | power(9.0, 3.0) | 729 |
power(a numeric, b numeric) | numeric | a raised to the power of b | power(9.0, 3.0) | 729 |
radians(dp) | dp | zamiana st. na rad | radians(45.0) | 0,79 |
random() | dp | liczba losowa z przedziału 0-1 | random() | - |
round(dp or numeric) | - | zaokrąglenie | round(42.4) | 42 |
round(v numeric, s integer) | numeric | zaokrągenie z podaną precyzją | round(42.4382, 2) | 42,44 |
setseed(dp) | integer | set seed for subsequent random() calls | setseed(0.54823) | 1177314959 |
sign(dp or numeric) | - | znak liczby | sign(-8.4) | -1 |
sqrt(dp or numeric) | - | pierwiastek | sqrt(2.0) | 1,41 |
trunc(dp or numeric) | - | liczba całkowita | trunc(42.8) | 42 |
trunc(v numeric, s integer) | numeric | obcięcie z dokładnością do.. | trunc(42.4382, 2) | 42,43 |