You are on page 1of 4

EXPERIMENT NO.

6
MOHAK KISHOR RAUT
TE - A - 29 (TU2F1819031)

AIM: To perform string and date-time operations on a given table in MySQL.

REQUIREMENTS: A PC with internet connection and MySQL software.

THEORY:
String Literal:
The string in MySQL is a sequence of characters or bytes that are enclosed in single quotes (') or
double quotes ("). For example, 'first string' and "second string" both are the same. The string of bytes
is called a binary string. Each binary string contains a character set and a collation.
Let us understand it with the help of an example. First, we are going to create a table named "Student"
that contains the following data:

Example

If we want to get the code and contact numbers whose name is Praful, execute the below statement:
SELECT roll_no, class FROM student WHERE name = 'Pritesh';  

OR,  

SELECT roll_no, class FROM student  WHERE name = "Pritesh";  

Output
It will give the following outputs where we can see that both queries give the same result, either uses
single quotes or double-quotes.

A string literals can also be used with special characters escape sequences. These special characters
are summarised in a tabular form below:

Escape Sequence Character Represented by Sequence


\0 It represents ASCII NULL character.
\b It represents a backspace character.
\n It represents a newline character.
\r It represents carriage return character.
\t It represents tab character.
\\ It represents a backslash (\) character.
\% It represents a % character.
\_ It represents a backslash character.
Date and Time

In MySQL programming, Date and Time literals are in the form of strings or numbers.

Following are some more formats in which date and time can be displayed.

EXAMPLE FORMAT
'2018-10-18' 'YYYY-MM-DD'
'20181018' 'YYYYMMDD'
20181018 YYYYMMDD
'18-10-18' 'YY-MM-DD'
181018 YYMMDD
'2018-10-18 5:25:20' 'YYYY-MM-DD HH:MM:SS'
'2018101852520' 'YYYYMMDDHHMMSS'
2018101852520 YYYYMMDDHHMMSS
'18-10-18 5:25:20' 'YY-MM-DD HH:MM:SS'
'18101852520' 'YYMMDDHHMMSS'
18101852520 YYMMDDHHMMSS
Example 1

select id, name, salary, date_format(sal_date,'%d-%m-%y') as new_date_formate from teacher;  

Example 2

select id, name, salary, date_format(sal_date,'%d%m%y') as new_date_formate from teacher;  

Date and Time Functions :-

Current_date :-
select current_date() as date;

Year :-
select current_date() as date, year(current_date()) as year ;
Month_name :-
select current_date() as date, monthname(current_date) as month_name ;

Day_name :-
select current_date() as date, dayname(current_date()) as day_name ;

Current_Timestamp :-
select current_timestamp() ;

String Functions :-
Lower :-
select lower("PRITESH SONAWANE") as Lower_case;

Upper :-
select upper("Pritesh Sonawane") as Upper_Case;
Concat :-
select concat("Pritesh"," Sonawane ") as Concatenation ;

Hex :-
select hex(255) as Hexadecimal_value ;

Insert:-
select insert("Priiesh",4, 1, 't') as inserted;

CONCLUSION:-
Here we successfully performed string, date and time functions on a table and observed the results.

You might also like