0% found this document useful (0 votes)
15 views22 pages

Numeric and String Functions in MySQL

This document contains examples of using various string and numeric functions in MySQL such as ABS, ACOS, ASIN, ATAN, CEIL, COS, LOG, MOD, PI, POW, ROUND and more. It shows the syntax of each function, the values or results returned when different parameters are passed to the functions. Aggregate functions like AVG, COUNT, MAX, MIN, SUM are also demonstrated on sample data from a 'details' table.

Uploaded by

Vishal Jindal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views22 pages

Numeric and String Functions in MySQL

This document contains examples of using various string and numeric functions in MySQL such as ABS, ACOS, ASIN, ATAN, CEIL, COS, LOG, MOD, PI, POW, ROUND and more. It shows the syntax of each function, the values or results returned when different parameters are passed to the functions. Aggregate functions like AVG, COUNT, MAX, MIN, SUM are also demonstrated on sample data from a 'details' table.

Uploaded by

Vishal Jindal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

mysql> use laptops;

Database changed

NUMERIC FUNCTIONS
mysql> select abs(-55);
+----------+
| abs(-55) |
+----------+
| 55 |
+----------+
1 row in set (0.01 sec)

mysql> select acos(10);


+----------+
| acos(10) |
+----------+
| NULL |
+----------+
1 row in set (0.01 sec)

mysql> select acos(1);


+---------+
| acos(1) |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)

mysql> select acos(-1);


+-------------------+
| acos(-1) |
+-------------------+
| 3.141592653589793 |
+-------------------+
1 row in set (0.00 sec)

mysql> select acos(-0.7);


+------------------+
| acos(-0.7) |
+------------------+
| 2.34619382340565 |
+------------------+
1 row in set (0.00 sec)
mysql> select asin(-1);
+---------------------+
| asin(-1) |
+---------------------+
| -1.5707963267948966 |
+---------------------+
1 row in set (0.00 sec)

mysql> select asin(-10);


+-----------+
| asin(-10) |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)

mysql> select atan(-10);


+---------------------+
| atan(-10) |
+---------------------+
| -1.4711276743037347 |
+---------------------+
1 row in set (0.00 sec)

mysql> select atan(2);


+--------------------+
| atan(2) |
+--------------------+
| 1.1071487177940904 |
+--------------------+
1 row in set (0.00 sec)

mysql> select atan2(10,12);


+--------------------+
| atan2(10,12) |
+--------------------+
| 0.6947382761967031 |
+--------------------+
1 row in set (0.00 sec)

mysql> select ceil(3.4);


+-----------+
| ceil(3.4) |
+-----------+
| 4 |
+-----------+
1 row in set (0.01 sec)

mysql> select cos(0.4);


+--------------------+
| cos(0.4) |
+--------------------+
| 0.9210609940028851 |
+--------------------+
1 row in set (0.00 sec)

mysql> select cos(1);


+--------------------+
| cos(1) |
+--------------------+
| 0.5403023058681398 |
+--------------------+
1 row in set (0.00 sec)

mysql> select log(10);


+-------------------+
| log(10) |
+-------------------+
| 2.302585092994046 |
+-------------------+
1 row in set (0.01 sec)

mysql> select log(1);


+--------+
| log(1) |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)

mysql> select log2(10);


+-------------------+
| log2(10) |
+-------------------+
| 3.321928094887362 |
+-------------------+
1 row in set (0.00 sec)
mysql> select log2(1);
+---------+
| log2(1) |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)

mysql> select mod(10,5);


+-----------+
| mod(10,5) |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec)

mysql> select mod(9,2);


+----------+
| mod(9,2) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)

mysql> select pi();


+----------+
| pi() |
+----------+
| 3.141593 |
+----------+
1 row in set (0.01 sec)

mysql> select pow(2,5);


+----------+
| pow(2,5) |
+----------+
| 32 |
+----------+
1 row in set (0.00 sec)

mysql> select cot(1);


+--------------------+
| cot(1) |
+--------------------+
| 0.6420926159343306 |
+--------------------+
1 row in set (0.01 sec)

mysql> select degrees(90);


+-------------------+
| degrees(90) |
+-------------------+
| 5156.620156177409 |
+-------------------+
1 row in set (0.01 sec)

mysql> select degrees(2);


+--------------------+
| degrees(2) |
+--------------------+
| 114.59155902616465 |
+--------------------+
1 row in set (0.00 sec)

mysql> select (6*3)div(3*3);


+---------------+
| (6*3)div(3*3) |
+---------------+
| 2 |
+---------------+
1 row in set (0.01 sec)

mysql> select floor(3.4);


+------------+
| floor(3.4) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)

mysql> select greatest("apple", "Apple");


+----------------------------+
| greatest("apple", "Apple") |
+----------------------------+
| Apple |
+----------------------------+
1 row in set (0.01 sec)
mysql> select least("apple", "Apple");
+-------------------------+
| least("apple", "Apple") |
+-------------------------+
| apple |
+-------------------------+
1 row in set (0.00 sec)

mysql> select ln(1);


+-------+
| ln(1) |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)

mysql> select ln(10);


+-------------------+
| ln(10) |
+-------------------+
| 2.302585092994046 |
+-------------------+
1 row in set (0.00 sec)

mysql> select radians(90);


+--------------------+
| radians(90) |
+--------------------+
| 1.5707963267948966 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();


+--------------------+
| rand() |
+--------------------+
| 0.5020185369085693 |
+--------------------+
1 row in set (0.00 sec)

mysql> select round(3.4);


+------------+
| round(3.4) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)

mysql> select round(3.4233432, 2);


+---------------------+
| round(3.4233432, 2) |
+---------------------+
| 3.42 |
+---------------------+
1 row in set (0.00 sec)

mysql> select sign(-5);


+----------+
| sign(-5) |
+----------+
| -1 |
+----------+
1 row in set (0.01 sec)

mysql> select sin(1);


+--------------------+
| sin(1) |
+--------------------+
| 0.8414709848078965 |
+--------------------+
1 row in set (0.00 sec)

mysql> select sqrt(4);


+---------+
| sqrt(4) |
+---------+
| 2 |
+---------+
1 row in set (0.01 sec)

mysql> select tan(2);


+--------------------+
| tan(2) |
+--------------------+
| -2.185039863261519 |
+--------------------+
1 row in set (0.01 sec)
mysql> select tan(1);
+--------------------+
| tan(1) |
+--------------------+
| 1.5574077246549023 |
+--------------------+
1 row in set (0.00 sec)

mysql> select truncate(3.4233432, 2);


+------------------------+
| truncate(3.4233432, 2) |
+------------------------+
| 3.42 |
+------------------------+
1 row in set (0.00 sec)
mysql> select avg(Price) from details;
+------------+
| avg(Price) |
+------------+
| 73166.6667 |
+------------+
1 row in set (0.01 sec)

mysql> select sum(Price) from details;


+------------+
| sum(Price) |
+------------+
| 439000 |
+------------+
1 row in set (0.00 sec)

mysql> select min(Price) from details;


+------------+
| min(Price) |
+------------+
| 34000 |
+------------+
1 row in set (0.00 sec)

mysql> select max(Price) from details;


+------------+
| max(Price) |
+------------+
| 132000 |
+------------+
1 row in set (0.00 sec)
mysql> select count(*) from details;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
1 row in set (0.01 sec)

mysql> select count(distinct Brand) from details;


+-----------------------+
| count(distinct Brand) |
+-----------------------+
| 4 |
+-----------------------+
1 row in set (0.00 sec)
STRING FUNCTIONS
mysql> select ascii('a');
+------------+
| ascii('a') |
+------------+
| 97 |
+------------+
1 row in set (0.01 sec)

mysql> select ascii('A');


+------------+
| ascii('A') |
+------------+
| 65 |
+------------+
1 row in set (0.00 sec)

//as you can see below, even if we give a string, only the first character’s ascii is given

mysql> select ascii('asdaf');


+----------------+
| ascii('asdaf') |
+----------------+
| 97 |
+----------------+
1 row in set (0.00 sec)
mysql> select char(65);
+--------------------+
| char(65) |
+--------------------+
| |
+--------------------+
1 row in set (0.00 sec)

mysql> select concat(Brand, " ", Name) from details;


+--------------------------+
| concat(Brand, " ", Name) |
+--------------------------+
| Lenovo Ideapad3 |
| HP Pavillion |
| Apple Macbook Air |
| Apple Macbook Pro |
| Asus Vivobook |
| Lenovo Slim5 |
+--------------------------+
6 rows in set (0.01 sec)

//the first operator in concat_ws is the separator used while concatenating the two strings

mysql> select concat_ws(',', Brand, Name) from details;


+-----------------------------+
| concat_ws(',', Brand, Name) |
+-----------------------------+
| Lenovo,Ideapad3 |
| HP,Pavillion |
| Apple,Macbook Air |
| Apple,Macbook Pro |
| Asus,Vivobook |
| Lenovo,Slim5 |
+-----------------------------+
6 rows in set (0.00 sec)
mysql> select format(Price, 1) from details;
+------------------+
| format(Price, 1) |
+------------------+
| 34,000.0 |
| 44,000.0 |
| 92,000.0 |
| 132,000.0 |
| 70,000.0 |
| 67,000.0 |
+------------------+
6 rows in set (0.01 sec)
mysql> select format(Price, 2) from details;
+------------------+
| format(Price, 2) |
+------------------+
| 34,000.00 |
| 44,000.00 |
| 92,000.00 |
| 132,000.00 |
| 70,000.00 |
| 67,000.00 |
+------------------+
6 rows in set (0.00 sec)

mysql> select insert("Name", 2, 3, "len");


+-----------------------------+
| insert("Name", 2, 3, "len") |
+-----------------------------+
| Nlen |
+-----------------------------+
1 row in set (0.00 sec)

mysql> select length(Name) from details;


+--------------+
| length(Name) |
+--------------+
| 8 |
| 9 |
| 11 |
| 11 |
| 8 |
| 5 |
+--------------+
6 rows in set (0.01 sec)

mysql> select locate("me", "Name");


+----------------------+
| locate("me", "Name") |
+----------------------+
| 3 |
+----------------------+
1 row in set (0.01 sec)
mysql> select reverse(Name) from details;
+---------------+
| reverse(Name) |
+---------------+
| 3dapaedI |
| noillivaP |
| riA koobcaM |
| orP koobcaM |
| kooboviV |
| 5milS |
+---------------+
6 rows in set (0.01 sec)

mysql> select rtrim("Nameeee ");


+------------------------+
| rtrim("Nameeee ") |
+------------------------+
| Nameeee |
+------------------------+
1 row in set (0.00 sec)

//You can see the length after rtrim is lesser than normal, which means all the blank spaces to the right are removed

mysql> select length(rtrim("Nameeee "));


+--------------------------------+
| length(rtrim("Nameeee ")) |
+--------------------------------+
| 7 |
+--------------------------------+
1 row in set (0.00 sec)

mysql> select length("Nameeee ");


+-------------------------+
| length("Nameeee ") |
+-------------------------+
| 13 |
+-------------------------+
1 row in set (0.00 sec)

mysql> select length(space(5));


+------------------+
| length(space(5)) |
+------------------+
| 5 |
+------------------+
1 row in set (0.00 sec)
mysql> select strcmp(Name, Brand) from details;
+---------------------+
| strcmp(Name, Brand) |
+---------------------+
| -1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
+---------------------+
6 rows in set (0.00 sec)

mysql> select substring_index("Apple", "p", 2);


+----------------------------------+
| substring_index("Apple", "p", 2) |
+----------------------------------+
| Ap |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select substring_index("Apple", "p", 1);


+----------------------------------+
| substring_index("Apple", "p", 1) |
+----------------------------------+
| A |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select lower(Brand) from details;


+--------------+
| lower(Brand) |
+--------------+
| lenovo |
| hp |
| apple |
| apple |
| asus |
| lenovo |
+--------------+
6 rows in set (0.01 sec)
mysql> select upper(Brand) from details;
+--------------+
| upper(Brand) |
+--------------+
| LENOVO |
| HP |
| APPLE |
| APPLE |
| ASUS |
| LENOVO |
+--------------+
6 rows in set (0.01 sec)

mysql> select length(" skyyy");


+---------------------+
| length(" skyyy") |
+---------------------+
| 9 |
+---------------------+
1 row in set (0.00 sec)

//ltrim also works in a similar way like rtrim

mysql> select length(ltrim(" skyyy"));


+----------------------------+
| length(ltrim(" skyyy")) |
+----------------------------+
| 5 |
+----------------------------+
1 row in set (0.01 sec)

//the trim function can be leading, trailing, or both

mysql> select trim(leading "k" from "kkangaroo");


+------------------------------------+
| trim(leading "k" from "kkangaroo") |
+------------------------------------+
| angaroo |
+------------------------------------+
1 row in set (0.00 sec)
mysql> select trim(both "k" from "kkangaroo");
+---------------------------------+
| trim(both "k" from "kkangaroo") |
+---------------------------------+
| angaroo |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select trim(both "o" from "ookkangaroo");
+-----------------------------------+
| trim(both "o" from "ookkangaroo") |
+-----------------------------------+
| kkangar |
+-----------------------------------+
1 row in set (0.00 sec)

mysql> select position("me" in "Name");


+--------------------------+
| position("me" in "Name") |
+--------------------------+
| 3 |
+--------------------------+
1 row in set (0.01 sec)

mysql> select left(Name, 2) from details;


+---------------+
| left(Name, 2) |
+---------------+
| Id |
| Pa |
| Ma |
| Ma |
| Vi |
| Sl |
+---------------+
6 rows in set (0.01 sec)

mysql> select right(Name, 2) from details;


+----------------+
| right(Name, 2) |
+----------------+
| d3 |
| on |
| ir |
| ro |
| ok |
| m5 |
+----------------+
6 rows in set (0.00 sec)
mysql> select replace("Lenovo", "L", "G");
+-----------------------------+
| replace("Lenovo", "L", "G") |
+-----------------------------+
| Genovo |
+-----------------------------+
1 row in set (0.01 sec)

mysql> select field("S", "A", "B", "c", "s", "r", "z");


+------------------------------------------+
| field("S", "A", "B", "c", "s", "r", "z") |
+------------------------------------------+
| 4 |
+------------------------------------------+
1 row in set (0.01 sec)

mysql> select char_length(Name) from details;


+-------------------+
| char_length(Name) |
+-------------------+
| 8 |
| 9 |
| 11 |
| 11 |
| 8 |
| 5 |
+-------------------+
6 rows in set (0.01 sec)

//if you give spaces in find_in_set it will not give the desired output

mysql> select find_in_set("S", "A, B, C, D, S, R, Z");


+-----------------------------------------+
| find_in_set("S", "A, B, C, D, S, R, Z") |
+-----------------------------------------+
| 0 |
+-----------------------------------------+
1 row in set (0.01 sec)

mysql> select find_in_set("S", "A,B,C,D,S,R,Z");


+-----------------------------------+
| find_in_set("S", "A,B,C,D,S,R,Z") |
+-----------------------------------+
| 5 |
+-----------------------------------+
1 row in set (0.00 sec)
mysql> select instr("Lenovo", "ov");
+-----------------------+
| instr("Lenovo", "ov") |
+-----------------------+
| 4 |
+-----------------------+
1 row in set (0.00 sec)

mysql> select lpad(Name, 30, "*") from details;


+--------------------------------+
| lpad(Name, 30, "*") |
+--------------------------------+
| **********************Ideapad3 |
| *********************Pavillion |
| *******************Macbook Air |
| *******************Macbook Pro |
| **********************Vivobook |
| *************************Slim5 |
+--------------------------------+
6 rows in set (0.00 sec)

mysql> select rpad(Name, 30, "*") from details;


+--------------------------------+
| rpad(Name, 30, "*") |
+--------------------------------+
| Ideapad3********************** |
| Pavillion********************* |
| Macbook Air******************* |
| Macbook Pro******************* |
| Vivobook********************** |
| Slim5************************* |
+--------------------------------+
6 rows in set (0.01 sec)

mysql> select Name, repeat(Name, 3) from details;


+-------------+-----------------------------------+
| Name | repeat(Name, 3) |
+-------------+-----------------------------------+
| Ideapad3 | Ideapad3Ideapad3Ideapad3 |
| Pavillion | PavillionPavillionPavillion |
| Macbook Air | Macbook AirMacbook AirMacbook Air |
| Macbook Pro | Macbook ProMacbook ProMacbook Pro |
| Vivobook | VivobookVivobookVivobook |
| Slim5 | Slim5Slim5Slim5 |
+-------------+-----------------------------------+
6 rows in set (0.00 sec)
mysql> select mid(Name, 2, 2) from details;
+-----------------+
| mid(Name, 2, 2) |
+-----------------+
| de |
| av |
| ac |
| ac |
| iv |
| li |
+-----------------+
6 rows in set (0.00 sec)

Date and Time function


mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2022-09-13 |
+------------+
1 row in set (0.01 sec)

mysql> select current_time();


+----------------+
| current_time() |
+----------------+
| 11:25:59 |
+----------------+
1 row in set (0.00 sec)

mysql> select current_timestamp();


+---------------------+
| current_timestamp() |
+---------------------+
| 2022-09-13 11:26:10 |
+---------------------+
1 row in set (0.01 sec)

mysql> select now();


+---------------------+
| now() |
+---------------------+
| 2022-09-13 11:57:11 |
+---------------------+
1 row in set (0.00 sec)
mysql> select adddate('2022-08-15', 5);
+--------------------------+
| adddate('2022-08-15', 5) |
+--------------------------+
| 2022-08-20 |
+--------------------------+
1 row in set (0.01 sec)

mysql> select adddate('2022-08-15', interval 1 month);


+-----------------------------------------+
| adddate('2022-08-15', interval 1 month) |
+-----------------------------------------+
| 2022-09-15 |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select adddate('2022-08-15', interval 1 week);


+----------------------------------------+
| adddate('2022-08-15', interval 1 week) |
+----------------------------------------+
| 2022-08-22 |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select sec_to_time(3600);


+-------------------+
| sec_to_time(3600) |
+-------------------+
| 01:00:00 |
+-------------------+
1 row in set (0.01 sec)

mysql> select to_days('2022-08-15');


+-----------------------+
| to_days('2022-08-15') |
+-----------------------+
| 738747 |
+-----------------------+
1 row in set (0.01 sec)

mysql> select datediff('2022-08-15','2022-08-20');


+-------------------------------------+
| datediff('2022-08-15','2022-08-20') |
+-------------------------------------+
| -5 |
+-------------------------------------+
1 row in set (0.01 sec)
mysql> select date_format('2022-08-15', '%d/%m/%y');
+---------------------------------------+
| date_format('2022-08-15', '%d/%m/%y') |
+---------------------------------------+
| 15/08/22 |
+---------------------------------------+
1 row in set (0.00 sec)

Conversion functions
mysql> select convert("2022-08-15", Date) as to_date;
+------------+
| to_date |
+------------+
| 2022-08-15 |
+------------+
1 row in set (0.01 sec)

mysql> select convert("2022-08-15", datetime) as to_date;


+---------------------+
| to_date |
+---------------------+
| 2022-08-15 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

mysql> select convert(201905, time) as to_time;


+----------+
| to_time |
+----------+
| 20:19:05 |
+----------+
1 row in set (0.00 sec)

mysql> select * from details;


+---------+-----------+--------+--------+---------+
| ID | Name | Brand | Price | OS |
+---------+-----------+--------+--------+---------+
| 2141101 | Ideapad3 | Lenovo | 34000 | Windows |
| 2141102 | Pavillion | HP | 44000 | Windows |
| 2141103 | Macbook | Apple | 92000 | MacOS |
| 2141104 | Macbook | Apple | 132000 | MacOS |
| 2141105 | Vivobook | Asus | 70000 | Windows |
| 2141106 | Slim5 | Lenovo | 67000 | Windows |
+---------+-----------+--------+--------+---------+
6 rows in set (0.03 sec)
mysql> select convert(price, time) as to_time from details;
+----------+
| to_time |
+----------+
| 03:40:00 |
| 04:40:00 |
| 09:20:00 |
| 13:20:00 |
| 07:00:00 |
| NULL |
+----------+
6 rows in set, 1 warning (0.00 sec)

mysql> select convert(price, char) as to_time from details;


+---------+
| to_time |
+---------+
| 34000 |
| 44000 |
| 92000 |
| 132000 |
| 70000 |
| 67000 |
+---------+
6 rows in set (0.01 sec)

mysql> select convert(price, char) as to_char from details;


+---------+
| to_char |
+---------+
| 34000 |
| 44000 |
| 92000 |
| 132000 |
| 70000 |
| 67000 |
+---------+
6 rows in set (0.00 sec)

mysql> select convert(6-1, signed) as to_signed;


+-----------+
| to_signed |
+-----------+
| 5 |
+-----------+
1 row in set (0.01 sec)
mysql> select convert(6-7, signed) as to_signed;
+-----------+
| to_signed |
+-----------+
| -1 |
+-----------+
1 row in set (0.00 sec)

mysql> select convert(6-7, unsigned) as to_signed;


+----------------------+
| to_signed |
+----------------------+
| 18446744073709551615 |
+----------------------+
1 row in set (0.00 sec)

mysql> select convert(18446744073709551615, signed) as to_signed;


+-----------+
| to_signed |
+-----------+
| -1 |
+-----------+
1 row in set (0.00 sec)

mysql> select convert(price, decimal(10,2)) as to_decimal from details;


+------------+
| to_decimal |
+------------+
| 34000.00 |
| 44000.00 |
| 92000.00 |
| 132000.00 |
| 70000.00 |
| 67000.00 |
+------------+
6 rows in set (0.01 sec)

mysql> notee

You might also like