Assignment 9 Study of in Built Functions in SQL - 181021004
Assignment 9 Study of in Built Functions in SQL - 181021004
THEORY:
Explain Functions and Basic types of functions.
Functions are simply pieces of code that perform some operations and then return a result.
Some functions accept parameters while other functions do not accept parameters.
The main idea behind functions is to have them stored in the database and avoid writing the
same code over and over again. We can control what is the input and define the structure/type of
output. We can call a function by simply using its name and providing the parameters needed.
SQL Server comes with a set of built-in functions that perform a variety of tasks.
MySQL comes bundled with a number of built-in functions. Built in functions are simply
functions come already implemented in the MySQL server. These functions allow us to perform
different types of manipulations on the data
Basic types of functions:
• Strings functions – operate on string data types
• Numeric functions – operate on numeric data types
• Date functions – operate on date data types
• Aggregate functions – operate on all the above data types and produce summarized result sets.
OUTPUT: (Copy Paste your output code here from your Mariadb Command Prompt, remove
error lines form code)
Enter password: *************
1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.3-MariaDB mariadb.org binary distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2
MariaDB [asst9]> select ascii(' ');
+------------+
| ascii(' ') |
+------------+
| 32 |
+------------+
1 row in set (0.000 sec)
3
+----------+
| 53 |
+----------+
1 row in set (0.026 sec)
4
MariaDB [asst9]> select char_length('database');
+-------------------------+
| char_length('database') |
+-------------------------+
| 8|
+-------------------------+
1 row in set (0.002 sec)
5
+-----------------------------------+
| ShivpriyaAmale |
+-----------------------------------+
1 row in set (0.031 sec)
6
MariaDB [asst9]> select upper('shivpriya');
+-----------------+
| upper(' shivpriya ') |
+-----------------+
| SHIVPRIYA |
+-----------------+
1 row in set (0.024 sec)
7
+--------------+
| ipo |
+--------------+
1 row in set (0.000 sec)
8
MariaDB [asst9]> select substring('abcdefghijklmnopqrstuvwxyz',21);
+--------------------------------------------+
| substring('abcdefghijklmnopqrstuvwxyz',21) |
+--------------------------------------------+
| uvwxyz |
+--------------------------------------------+
1 row in set (0.001 sec)
MariaDB [asst9]> select substring('abcdefghijklmnopqrstuvwxyz' from 21);
+-------------------------------------------------+
| substring('abcdefghijklmnopqrstuvwxyz' from 21) |
+-------------------------------------------------+
| uvwxyz |
+-------------------------------------------------+
1 row in set (0.000 sec)
9
| uvw |
+-------------------------------------------------------+
1 row in set (0.000 sec)
10
MariaDB [asst9]> select strcmp ('abcde','abcd');
+-------------------------+
| strcmp ('abcde','abcd') |
+-------------------------+
| 1|
+-------------------------+
1 row in set (0.000 sec)
11
| hello!!! |
+----------------------+
1 row in set (0.033 sec)
12
MariaDB [asst9]> select reverse ('abcd');
+------------------+
| reverse ('abcd') |
+------------------+
| dcba |
+------------------+
1 row in set (0.000 sec)
MariaDB [asst9]>
MariaDB [asst9]> select replace ('online','on','off');
+-------------------------------+
| replace ('online','on','off') |
+-------------------------------+
| offline |
+-------------------------------+
1 row in set (0.001 sec)
13
| locate ('sanjay','ShivpriyaSanjayAmale') |
+------------------------------------------+
| 10 |
+------------------------------------------+
1 row in set (0.001 sec)
14
1 row in set (0.001 sec)
MariaDB [asst9]>
LIST of FUNCTIONS:
ascii() Returns the numeric value of the leftmost character of the string str.
Returns 0 if str is the empty string. Returns NULL if str is NULL.
ASCII() works for characters with numeric values from 0 to 255.
bin() Returns a string representation of the binary value of N
char_length() Returns the length of the string str measured in characters. A multi-byte
character counts as a single character.
Concat() Returns the string that results from concatenating the arguments. May
have one or more arguments.
CONCAT_WS() CONCAT_WS() stands for Concatenate With Separator and is a
special form of CONCAT(). The first argument is the separator for the
rest of the arguments. The separator is added between the strings to be
15
concatenated. The separator can be a string, as can the rest of the
arguments. If the separator is NULL, the result is NULL.
ELT(N,str1,str2,str3,...) Returns str1 if N = 1, str2 if N = 2, and so on. Returns NULL if N is
less than 1 or greater than the number of arguments. ELT() is the
complement of FIELD().
UPPER(str)/ucase() Returns the string str with all characters changed to uppercase
according to the current character set mapping.
Lower()/lcase Returns the string str with all characters changed to lowercase
according to the current character set mapping.
Trim(){BOTH | LEADING | Returns the string str with all remstr prefixes or suffixes removed. If
TRAILING none of the specifiers BOTH, LEADING, or TRAILING is given,
RTRIM(str) BOTH is assumed. remstr is optional and, if not specified, spaces are
LTRIM(str) removed.
Returns the string str, left-padded with the string padstr to a length of
len characters. If str is longer than len, the return value is shortened to
len characters.
RIGHT(str,len) Returns the rightmost len characters from the string str, or NULL if any
argument is NULL.
LEFT(str,len)
Returns the leftmost len characters from the string str, or NULL if any
argument is NULL.
REVERSE(str) Returns the string str with the order of the characters reversed.
REPLACE(str,from_str,to_str) Returns the string str with all occurrences of the string from_str
replaced by the string to_str. REPLACE() performs a case-sensitive
match when searching for from_str.
REPEAT(str,count) Returns a string consisting of the string str repeated count times. If
count is less than 1, returns an empty string. Returns NULL if str or
count are NULL.
16
LOCATE(substr,str) Returns the position of the first occurrence of substring substr in string
str.
LENGTH(str) Returns the length of the string str, measured in bytes.
INSERT(str,pos,len,newstr) Returns the string str, with the substring beginning at position pos and
len characters long replaced by the string newstr. Returns the original
string if pos is not within the length of the string. Replaces the rest of
the string from position pos if len is not within the length of the rest of
the string. Returns NULL if any argument is NULL.
INSTR(str,substr) Returns the position of the first occurrence of substring substr in string
str.
UNHEX(str) Performs the inverse operation of HEX(str). That is, it interprets each
pair of hexadecimal digits in the argument as a number and converts it
to the character represented by the number. The resulting characters are
returned as a binary string.
17
18
19
20
(Please Note: A viva will be taken in November based on your individual assignments. Please
do not copy paste the contents from others writeups.)
*******End of the Assignment*******
21