Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

EXPERIMENT – 11

VARUN
2K19/SE/142
Aim: To study commands for String Functions (ASCII, Lower, Upper, Length,
Initcap, Translate, Substr) and Execute queries using these commands.
Queries Used:
ASCII: The ASCII() function returns the ASCII value of the leftmost character of
a character expression.
ASCII(character)
LOWER: The LOWER() function converts a string to lower-case.
LOWER(string)

UPPER: The UPPER() function converts a string to upper-case.


UPPER(string)

LENGTH: Returns the length of the string in bytes.


LENGTH(string)

SUBSTR: This function extracts some characters from the string based on the
stating index and length give.
SUBSTR(string, starting_position, length)
Starting position is the index from which characters are to be extracted and the
length is the length from the starting index up to which characters are to be
extracted.

TRANSLATE (Not available in MySQL): It returns expr with all occurrences of


each character in from_string replaced by its corresponding character in to_string.
TRANSLATE(string_to_be_converted(expr), from_string, to_string)

Characters in expr that are not in from_string are not replaced. If expr is a
character string, then you must enclose it in single quotation marks. The
argument from_string can contain more characters than to_string. Also, we cannot
use an empty string for to_string to remove all characters in from_string from the
return value. Oracle Database interprets the empty string as null, and if this
function has
a null argument, then it returns null. Translate provides functionality related to
that provided by the Replace function. Replace lets you substitute a single string
for another single string, as well as remove character strings. Translate lets you
make several single-character, one-to-one substitutions in one operation.

INITCAP (Not available in MySQL): It returns char, with the first letter of each
word in uppercase, all other letters in lowercase. Words are delimited by white
space or characters that are not alphanumeric.
INITCAP(string or char)

Char can be of any of the datatypes CHAR, VARCHAR2, NCHAR, or


NVARCHAR. The return value is the same datatype as char.

Example:

CREATE TABLE persons(id INT NOT NULL PRIMARY


KEY, last_name varchar(255) NOT NULL,first_name
varchar(255),age INT);

INSERT INTO persons VALUES(1, "Agarwal", "Vaibhav",


20);
INSERT INTO persons VALUES(2, "Gupta", "Vedant", 15);
INSERT INTO persons VALUES(3, "Agarwal", "Vaishnavi",
16);
INSERT INTO persons VALUES(4, "Gupta", "Sakaar", 12);
INSERT INTO persons VALUES(5, "Agarwal", "Anshika",
20);
INSERT INTO persons VALUES(6, "Agarwal", "Vinemra", 16);

SELECT first_name, LOWER( first_name ) FROM persons;

SELECT last_name, UPPER( last_name ) FROM persons;

SELECT last_name, ASCII( last_name ) FROM persons;

SELECT first_name, LENGTH( first_name ) FROM persons;


SELECT first_name, SUBSTR( first_name, 0, 4) FROM persons;

SELECT last_name, TRANSLATE( last_name, 'al', '**') FROM persons;

SELECT first_name, INITCAP( first_name ) FROM persons;

Output:
Note: We ran all of these queries on an online SQL compiler with oracle
database
Conclusion:
Learnt the use of string functions (Upper, Lower, Length, Substr, ASCII,
Translate, Initcap).

You might also like