Chapter 10 SQL Functions
Chapter 10 SQL Functions
MySQL – Functions
UPPER() or UCASE()
Converts given string in upper case.
UPPER (Str)
mysql> SELECT UPPER(‘abcD’ ) FROM DUAL;
ABCD
mysql> SELECT UPPER(Name) FROM Student;
mysql> SELECT UCASE(Fname) FROM Student;
String Functions cont…
LTRIM()
Returns string after removing leading spaces.
mysql> SELECT LTRIM(‘ abcd’ ) FROM DUAL;
abcd
mysql> SELECT LTRIM(Name) FROM Student;
RTRIM()
Returns string after removing trailing spaces.
mysql> SELECT RTRIM(‘abcd ’ ) FROM DUAL;
abcd
mysql> SELECT RTRIM(Name) FROM Student;
TRIM()
Returns string after removing leading and trailing spaces.
mysql> SELECT TRIM(‘ abcd ’ ) FROM DUAL;
abcd
String Functions cont…
SUBSTR()
Returns a sub string of given length from specified position.
SUBSTR (Str, position [,length])
mysql> SELECT SUBSTR(‘MY COMPUTER’, 4,3’ ) COM
If position is negative then backward position is counted.
mysql> SELECT SUBSTR(‘ABCDEFG’ , -5, 4) FROM Student;
CDEF
If Length is omitted then up to end of the string is considered.
mysql> SELECT SUBSTR(‘ABCDEFG’ , 3) FROM Student;
CDEFG
INSTR()
Searches a string in to another string and returns its position.
INSTR(Str1, Str2)
mysql> SELECT INSTR(‘CORPORATE FLOOR’, ‘OR’); 2
mysql> SELECT Name, INSTR(Name,’a’) FROM Student;
String Functions cont…
LEFT()
Returns leftmost string up to given length.
LEFT (Str , length)
mysql> SELECT LEFT(‘MYSQL’, 2 ) MY
mysql> SELECT LEFT( Name, 4) FROM Student;
RIGHT()
Returns rightmost string up to given length.
RIGHT (Str , length)
mysql> SELECT RIGHT(‘MYSQL’, 3 ) SQL
mysql> SELECT RIGHT (Name, 4) FROM Student;
MID()
Returns a substring upto given length from given position.
MID (Str ,Pos, Length) Mid() is
mysql> SELECT MID(‘COMPUTER’, 4,3 ) PUT similar to
Substr()
mysql> SELECT MID (Name, 4,3) FROM Student;
Summery of String Functions
Name Purpose Example
CONCAT(str1,str2) Returns concatenated string i.e. Select CONCAT(Name, City)
str1+str2. from Student;
LOWER(str) / Returns the given string in lower Select LOWER(‘ABC’); abc
LCASE(str) case.
SYSDATE()
Returns current date and time as YYYY-MM-DD HH:MM:SS
mysql> SELECT SYSDATE() ;
2014-01-30 10:30:20
NOW()
Returns current date and time as YYYY-MM-DD HH:MM:SS
mysql> SELECT SYSDATE() FROM DUAL
2010-01-30 10:30:20
MONTH()
Returns month of the given date expression.
MONTH (Dt)
mysql> SELECT MONTH(‘2008-12-31’) ; 12
mysql> SELECT MONTH( CURDATE());
Date & Time Functions cont…
DAYOFMONTH()
Returns day of month of the given date expression.
DAYOFMONTH (Dt)
mysql> SELECT DAYOFMONTH(‘2008-12-31’) ;
31
mysql> SELECT DAYOFMONTH( CURDATE()) ;
mysql> SELECT DAYOFMONTH( DOB) FROM Student;
DAYNAME()
Returns the name of Week day of the given date expression.
DAYNAME (Dt)
mysql> SELECT DAYNAME(‘2008-12-31’) ;
SUNDAY
mysql> SELECT DAYNAME( CURDATE()) ;
mysql> SELECT DAYNAME( DOB) FROM Student;
Date & Time Functions cont…
DAYOFWEEK()
Returns day of week i.e. 1- Sunday, 2- Tuesday.. etc. of given
date.
DAYOFWEEK (Dt)
mysql> SELECT DAYOFWEEK(‘2008-12-31’) ;
1
mysql> SELECT DAYOFWEEK(CURDATE()) ;
DAYOFYEAR()
Returns the day of year of the given date expression.
DAYOFYAER (Dt)
mysql> SELECT DAYOFYAER(‘2010-02-05’) ;
36
mysql> SELECT DAYOFYAER( CURDATE()) ;
mysql> SELECT DAYOFYEAR( DOB) FROM Student;
Summery of Date & Time Functions
Name Purpose Example
CURDATE() / Returns the current date in Select CURDATE();
CURRENT_DATE() YYYY-MM-DD format. 2013-10-02