PostgreSQL Functions
Build-in Functions:
- Aggregate functions
- Scalar Functions
Aggregate Functions:
Examples:pearl=# select * from siva ;
id | name | age | salary
----+----------+-----+--------
1 | muthu | 21 | 2000
2 | siva | 26 | 4000
3 | thanigai | 27 | 5000
4 | chandru | 28 | 2000
5 | kathir | 27 | 3000
(5 rows)
pearl=# select max(age) from siva;
max
-----
28
(1 row)
pearl=# select min(age) from siva;
min
-----
21
(1 row)
pearl=# select avg(salary) from siva;
avg
-----------------------
3200.0000000000000000
(1 row)
pearl=# select count(id) from siva;
count
-------
5
(1 row)
pearl=# select sum(salary) from siva;
sum
-------
16000
(1 row)
pearl=# select * from siva order by salary;
id | name | age | salary
----+----------+-----+--------
1 | muthu | 21 | 2000
4 | chandru | 28 | 2000
5 | kathir | 27 | 3000
2 | siva | 26 | 4000
3 | thanigai | 27 | 5000
(5 rows)
pearl=# select * from siva order by salary desc;
id | name | age | salary
----+----------+-----+--------
3 | thanigai | 27 | 5000
2 | siva | 26 | 4000
5 | kathir | 27 | 3000
1 | muthu | 21 | 2000
4 | chandru | 28 | 2000
(5 rows)
pearl=# select age from siva group by age;
age
-----
26
21
28
27
(4 rows)
pearl=# select age from siva group by age having age=27;
age
-----
27
(1 row)
Scalar Functions:
date functions:
pearl=# select age(timestamp '2001-04-10', timestamp '1957-06-13');age
-------------------------
43 years 9 mons 27 days
(1 row)
pearl=# select age(timestamp '2001-04-10');
age
-------------------------
19 years 3 mons 18 days
(1 row)
pearl=# select clock_timestamp();
clock_timestamp
----------------------------------
2020-07-28 11:58:29.142519+05:30
(1 row)
pearl=# select current_date;
current_date
--------------
2020-07-28
(1 row)
pearl=# select current_time;
current_time
-----------------------
11:58:53.033972+05:30
(1 row)
pearl=# select current_timestamp;
current_timestamp
---------------------------------
2020-07-28 11:58:57.95539+05:30
(1 row)
pearl=# select date_part('hour', timestamp '2001-02-16 20:38:40');
date_part
-----------
20
(1 row)
pearl=# select date_part('month', interval '2 years 3 months');
date_part
-----------
3
(1 row)
pearl=# select date_trunc('hour', timestamp '2001-02-16 20:38:40');
date_trunc
---------------------
2001-02-16 20:00:00
(1 row)
pearl=# select date_trunc('hour', interval '2 days 3 hours 40 minutes');
date_trunc
-----------------
2 days 03:00:00
(1 row)
pearl=# select localtime;
localtime
-----------------
12:03:06.762483
(1 row)
pearl=# select localtimestamp;
localtimestamp
---------------------------
2020-07-28 12:03:17.96969
(1 row)
pearl=# select make_date(2013, 7, 15);
make_date
------------
2013-07-15
(1 row)
pearl=# select make_time(8, 15, 23.5);
make_time
------------
08:15:23.5
(1 row)
pearl=# select make_timestamp(2013, 7, 15, 8, 15, 23.5);
make_timestamp
-----------------------
2013-07-15 08:15:23.5
(1 row)
pearl=# select now();
now
----------------------------------
2020-07-28 12:04:34.995758+05:30
(1 row)
pearl=# select statement_timestamp();
statement_timestamp
----------------------------------
2020-07-28 12:04:54.696116+05:30
(1 row)
pearl=# select timeofday();
timeofday
-------------------------------------
Tue Jul 28 12:05:13.588796 2020 IST
(1 row)
pearl=# select transaction_timestamp();
transaction_timestamp
----------------------------------
2020-07-28 12:05:57.968226+05:30
(1 row)
pearl=# SELECT to_date('20170103','YYYYMMDD');
to_date
------------
2017-01-03
(1 row)
pearl=# SELECT TO_TIMESTAMP('2017-03-31 9:30:20','YYYY-MM-DD HH:MI:SS');
to_timestamp
---------------------------
2017-03-31 09:30:20+05:30
(1 row)
numeric functions:
pearl=# select abs(5);abs
-----
5
(1 row)
pearl=# select abs(-5);
abs
-----
5
(1 row)
pearl=# select ceil(7.43);
ceil
------
8
(1 row)
pearl=# select ceiling(7.43);
ceiling
---------
8
(1 row)
pearl=# select floor(7.43);
floor
-------
7
(1 row)
pearl=# select greatest(4,3,5,6,2,8,1);
greatest
----------
8
(1 row)
pearl=# select least(4,3,5,6,2,8,1);
least
-------
1
(1 row)
pearl=# select mod(15,4);
mod
-----
3
(1 row)
pearl=# select pi();
pi
------------------
3.14159265358979
(1 row)
pearl=# select power(4,4);
power
-------
256
(1 row)
pearl=# select pow(4,4);
pow
-----
256
(1 row)
pearl=# select round(6.68456);
round
-------
7
(1 row)
pearl=# select round(6.68456,2);
round
-------
6.68
(1 row)
pearl=# select trunc(123.9);
trunc
-------
123
(1 row)
pearl=# select sqrt(81);
sqrt
------
9
(1 row)
string functions:
pearl=# select ascii('A');ascii
-------
65
(1 row)
pearl=# select char_length('muthu');
char_length
-------------
5
(1 row)
pearl=# select length('siva');
length
--------
4
(1 row)
pearl=# select concat('muthu','siva');
concat
-----------
muthusiva
(1 row)
pearl=# select concat_ws(',','first_name','second_name');
concat_ws
------------------------
first_name,second_name
(1 row)
pearl=# select left('muthusiva',5);
left
-------
muthu
(1 row)
pearl=# select right('muthusiva',4);
right
-------
siva
(1 row)
pearl=# select lower('MUTHU');
lower
-------
muthu
(1 row)
pearl=# select upper('muthu');
upper
-------
MUTHU
(1 row)
pearl=# select ltrim(' siva');
ltrim
--------
siva
(1 row)
pearl=# select rtrim('siva ');
rtrim
-------
siva
(1 row)
pearl=# select trim(' siva ');
btrim
-------
siva
(1 row)
pearl=# select trim(leading 'x' from 'xxxsivaxxx');
ltrim
---------
sivaxxx
(1 row)
pearl=# select trim(both 'x' from 'xxxsivaxxx');
btrim
-------
siva
(1 row)
pearl=# select trim(trailing 'x' from 'xxxsivaxxx');
rtrim
---------
xxxsiva
(1 row)
pearl=# SELECT REGEXP_SPLIT_TO_ARRAY('muthu siva', E'\\s+');
regexp_split_to_array
-----------------------
{muthu,siva}
(1 row)
pearl=# SELECT REGEXP_SPLIT_TO_TABLE('muthu siva', E'\\s+');
regexp_split_to_table
-----------------------
muthu
siva
(2 rows)
pearl=# select repeat('siva',3);
repeat
--------------
sivasivasiva
(1 row)
pearl=# select replace('muthu siva','muthu','pearl');
replace
------------
pearl siva
(1 row)
pearl=# select reverse('avisuhtum');
reverse
-----------
muthusiva
(1 row)
pearl=# select rpad('muthu',10,'*');
rpad
------------
muthu*****
(1 row)
pearl=# select lpad('muthu',10,'*');
lpad
------------
*****muthu
(1 row)
pearl=# select substring('iammuthusiva',4);
substring
-----------
muthusiva
(1 row)
pearl=# select substring('iammuthusiva' from 4);
substring
-----------
muthusiva
(1 row)
pearl=# select substring('iammuthusiva',4,5);
substring
-----------
muthu
(1 row)
conversion functions:
pearl=# select to_char(current_timestamp, 'HH12:MI:SS');to_char
----------
12:55:02
(1 row)
pearl=# select to_char(interval '15h 2m 12s', 'HH24:MI:SS');
to_char
----------
15:02:12
(1 row)
pearl=# select to_char(125, '999');
to_char
---------
125
(1 row)
pearl=# select to_date('05 Dec 2000', 'DD Mon YYYY');
to_date
------------
2000-12-05
(1 row)
pearl=# select to_number('12,454.8-', '99G999D9S'); //visite https://www.postgresqltutorial.com/postgresql-to_number/
to_number
-----------
-12454.8
(1 row)
pearl=# select to_timestamp('05 Dec 2000', 'DD Mon YYYY');
to_timestamp
---------------------------
2000-12-05 00:00:00+05:30
(1 row)
Comments
Post a Comment