0% found this document useful (0 votes)
30 views

Assignment 9 Study of in Built Functions in SQL - 181021004

The document discusses functions in SQL and provides examples of using various string, numeric, and other functions in MariaDB such as ASCII, BIN, CHAR_LENGTH, CONCAT, SUBSTRING, LTRIM, RTRIM, etc. It shows the output of running these functions in MariaDB queries.

Uploaded by

Shivpriya Amale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Assignment 9 Study of in Built Functions in SQL - 181021004

The document discusses functions in SQL and provides examples of using various string, numeric, and other functions in MariaDB such as ASCII, BIN, CHAR_LENGTH, CONCAT, SUBSTRING, LTRIM, RTRIM, etc. It shows the output of running these functions in MariaDB queries.

Uploaded by

Shivpriya Amale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

ASSIGNMENT 9(SUBJECT: DBMS)

(Please use this format for doing assignments)


SUBMISSION DATE: 10-11-2021
NAME OF STUDENT: Shivpriya Amale
BRANCH: Mechanical
ID: 181021004

AIM: Study of Functions in SQL.


TOOL: MariaDB (If any other tool, please mention the name)
PROGRAMMING LANGUAGE: Structured Query language (SQL)

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

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database asst9;


Query OK, 1 row affected (0.213 sec)

MariaDB [(none)]> use asst9;


Database changed
MariaDB [asst9]> select ascii('v');
+------------+
| ascii('v') |
+------------+
| 118 |
+------------+
1 row in set (0.183 sec)

MariaDB [asst9]> select ascii('V');


+------------+
| ascii('V') |
+------------+
| 86 |
+------------+
1 row in set (0.001 sec)

2
MariaDB [asst9]> select ascii(' ');
+------------+
| ascii(' ') |
+------------+
| 32 |
+------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select ascii('');


+-----------+
| ascii('') |
+-----------+
| 0|
+-----------+
1 row in set (0.000 sec)

MariaDB [asst9]> select ascii(null);


+-------------+
| ascii(null) |
+-------------+
| NULL |
+-------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select ascii(5);


+----------+
| ascii(5) |

3
+----------+
| 53 |
+----------+
1 row in set (0.026 sec)

MariaDB [asst9]> select bin(4);


+--------+
| bin(4) |
+--------+
| 100 |
+--------+
1 row in set (0.047 sec)

MariaDB [asst9]> select bin(3);


+--------+
| bin(3) |
+--------+
| 11 |
+--------+
1 row in set (0.000 sec)

MariaDB [asst9]> select bin(null);


+-----------+
| bin(null) |
+-----------+
| NULL |
+-----------+
1 row in set (0.000 sec)

4
MariaDB [asst9]> select char_length('database');
+-------------------------+
| char_length('database') |
+-------------------------+
| 8|
+-------------------------+
1 row in set (0.002 sec)

MariaDB [asst9]> select concat('shivpriya''amale');


+----------------------------+
| concat('shivpriya''amale') |
+----------------------------+
| shivpriya'amale |
+----------------------------+
1 row in set (0.003 sec)

MariaDB [asst9]> select concat('shivpriya','amale');


+-----------------------------+
| concat('shivpriya','amale') |
+-----------------------------+
| shivpriyaamale |
+-----------------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select concat_ws('','Shivpriya','Amale');


+-----------------------------------+
| concat_ws('','Shivpriya','Amale') |

5
+-----------------------------------+
| ShivpriyaAmale |
+-----------------------------------+
1 row in set (0.031 sec)

MariaDB [asst9]> select concat_ws(' ','Shivpriya','Amale');


+------------------------------------+
| concat_ws(' ','Shivpriya','Amale') |
+------------------------------------+
| Shivpriya Amale |
+------------------------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select elt(1,'ab','tpo','ipo','pco');


+-------------------------------+
| elt(1,'ab','tpo','ipo','pco') |
+-------------------------------+
| ab |
+-------------------------------+
1 row in set (0.039 sec)

MariaDB [asst9]> select elt(3,'ab','tpo','ipo','pco');


+-------------------------------+
| elt(3,'ab','tpo','ipo','pco') |
+-------------------------------+
| ipo |
+-------------------------------+
1 row in set (0.001 sec)

6
MariaDB [asst9]> select upper('shivpriya');
+-----------------+
| upper(' shivpriya ') |
+-----------------+
| SHIVPRIYA |
+-----------------+
1 row in set (0.024 sec)

MariaDB [asst9]> select ucase(' shivpriya ');


+-----------------+
| ucase(' shivpriya ') |
+-----------------+
| SHIVPRIYA |
+-----------------+
1 row in set (0.001 sec)

MariaDB [asst9]> select lower('IPO');


+--------------+
| lower('IPO') |
+--------------+
| ipo |
+--------------+
1 row in set (0.001 sec)

MariaDB [asst9]> select lcase('IPO');


+--------------+
| lcase('IPO') |

7
+--------------+
| ipo |
+--------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select trim(' ok ');


+----------------+
| trim(' ok ') |
+----------------+
| ok |
+----------------+
1 row in set (0.252 sec)

MariaDB [asst9]> select ltrim('ok');


+-------------+
| ltrim('ok') |
+-------------+
| ok |
+-------------+
1 row in set (0.001 sec)

MariaDB [asst9]> select rtrim('ok');


+-------------+
| rtrim('ok') |
+-------------+
| ok |
+-------------+
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)

MariaDB [asst9]> select substring('abcdefghijklmnopqrstuvwxyz', 21,3);


+-----------------------------------------------+
| substring('abcdefghijklmnopqrstuvwxyz', 21,3) |
+-----------------------------------------------+
| uvw |
+-----------------------------------------------+
1 row in set (0.037 sec)

MariaDB [asst9]> select substring('abcdefghijklmnopqrstuvwxyz' from 21 for 3);


+-------------------------------------------------------+
| substring('abcdefghijklmnopqrstuvwxyz' from 21 for 3) |
+-------------------------------------------------------+

9
| uvw |
+-------------------------------------------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select strcmp ('abcd','abcd');


+------------------------+
| strcmp ('abcd','abcd') |
+------------------------+
| 0|
+------------------------+
1 row in set (0.017 sec)

MariaDB [asst9]> select strcmp ('abcd','abce');


+------------------------+
| strcmp ('abcd','abce') |
+------------------------+
| -1 |
+------------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select strcmp ('abcd','abc');


+-----------------------+
| strcmp ('abcd','abc') |
+-----------------------+
| 1|
+-----------------------+
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)

MariaDB [asst9]> select strcmp ('abc','abce');


+-----------------------+
| strcmp ('abc','abce') |
+-----------------------+
| -1 |
+-----------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select space(10);


+------------+
| space(10) |
+------------+
| |
+------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select rpad('hello', 8,'!');


+----------------------+
| rpad('hello', 8,'!') |
+----------------------+

11
| hello!!! |
+----------------------+
1 row in set (0.033 sec)

MariaDB [asst9]> select lpad('hello', 8,'!');


+----------------------+
| lpad('hello', 8,'!') |
+----------------------+
| !!!hello |
+----------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select right ('abcd',3);


+------------------+
| right ('abcd',3) |
+------------------+
| bcd |
+------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select left ('abcd',2);


+-----------------+
| left ('abcd',2) |
+-----------------+
| ab |
+-----------------+
1 row in set (0.000 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)

MariaDB [asst9]> select repeat ('abc',2);


+------------------+
| repeat ('abc',2) |
+------------------+
| abcabc |
+------------------+
1 row in set (0.192 sec)

MariaDB [asst9]> select locate ('sanjay','ShivpriyaSanjayAmale');


+------------------------------------------+

13
| locate ('sanjay','ShivpriyaSanjayAmale') |
+------------------------------------------+
| 10 |
+------------------------------------------+
1 row in set (0.001 sec)

MariaDB [asst9]> select length ('Shivpriya');


+----------------------+
| length ('Shivpriya') |
+----------------------+
| 9|
+----------------------+
1 row in set (0.001 sec)

MariaDB [asst9]> select insert ('123456789',3,10,'abcd');


+----------------------------------+
| insert ('123456789',3,10,'abcd') |
+----------------------------------+
| 12abcd |
+----------------------------------+
1 row in set (0.000 sec)

MariaDB [asst9]> select instr ('Shivpriya_Sanjay_Amale','sanjay');


+-------------------------------------------+
| instr ('Shivpriya_Sanjay_Amale','sanjay') |
+-------------------------------------------+
| 11 |
+-------------------------------------------+

14
1 row in set (0.001 sec)

MariaDB [asst9]> select hex ('Shivpriya');


+--------------------+
| hex ('Shivpriya') |
+--------------------+
| 536869767072697961 |
+--------------------+
1 row in set (0.001 sec)

MariaDB [asst9]> select unhex ('536869767072697961');


+------------------------------+
| unhex ('536869767072697961') |
+------------------------------+
| Shivpriya |
+------------------------------+
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 with trailing space characters removed.


Returns the string str with leading space characters removed.
SUBSTRING(str,pos) The forms without a len argument return a substring from string str
SUBSTRING(str FROM pos) starting at position pos. The forms with a len argument return a
SUBSTRING(str,pos,len) substring len characters long from string str, starting at position pos.
SUBSTRING(str FROM pos The forms that use FROM are standard SQL syntax. It is also possible
FOR len) to use a negative value for pos. In this case, the beginning of the
substring is pos characters from the end of the string, rather than the
beginning. A negative value may be used for pos in any of the forms of
this function.
STRCMP(str1, str2) Compares two strings and returns 0 if both strings are equal, it returns
-1 if the first argument is smaller than the second according to the
current sort order otherwise it returns 1.
SPACE(N) Returns a string consisting of N space characters.
RPAD(str,len,padstr) Returns the string str, right-padded with the string padstr to a length of
len characters. If str is longer than len, the return value is shortened to
LPAD(str,len,padstr) len characters.

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.

SNAPSHOTS OF YOUR CODE:

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

You might also like