Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

Chapter 6 - MySQL Function

The document discusses various string, numeric and date/time functions in MySQL. It explains functions like CHAR, CONCAT, LOWER, SUBSTR, MOD, POWER, ROUND, CURDATE, DATE, MONTH, DAY and YEAR along with their syntax and usage.

Uploaded by

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

Chapter 6 - MySQL Function

The document discusses various string, numeric and date/time functions in MySQL. It explains functions like CHAR, CONCAT, LOWER, SUBSTR, MOD, POWER, ROUND, CURDATE, DATE, MONTH, DAY and YEAR along with their syntax and usage.

Uploaded by

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

Chapter - 6

6.1 Introduction
 Date can be manipulated in queries through functions supported by
SQL.
 “ A function is a special type of predefined command set that
performs some operation and returns a single value.”
 Functions operate on zero, one two or more values that are provided
to them.
 The values that are provided to functions are called parameters or
arguments.
 The MySQL functions have been categorised into various categorised,
such as Text functions, Mathematical functions , Date and Time
functions and so on.
6.2 Text/String Function
 The String functions of MySQL can manipulate the text string in
many ways.Some commonly used string functions are as follows:
1. CHAR( )
This function returns the character for each integer passed.
Syntax
CHAR(value 1,value 2)
 Returns a string made up of the ASCII representation of the decimal
value list.
 String in numeric format are converted to a decimal value.
 Null values are ignored.
 Argument type : Integer Return Value : String
Example 1

Write a query to create a string from the ASCII values 70,65,67,69


mysql > SELECT CHAR( 70,65,67,69);
2. CONCAT

 This function concatenates two string.


Syntax
CONCAT( str1, str2, . . . . )
 Return argument str1 concatenated with argument str2.
Argument types : String,String Return value : String
Example 3

mysql>SELECT CONCAT ( -
>name ,aggregate) As “Name -
>Marks” FROM student
->WHERE age = 14 OR age = 16;
Example 3

mysql>SELECT CONCAT ( -
>name ,aggregate) As “Name -
>Marks” FROM student
->WHERE age = 14 OR age = 16;
3. LOWER / LCASE

 This function converts a String into lowercase.


Syntax
Lower( str )
LCASE( str )
 Returns the argument str, with all letters in lowercase.
 The return value has the same data type as the argument
char( CHAR or VARCHAR )

Argument type : String Return value : String


4. SUBSTR

 This function extracts a substring from a given string.


Syntax
SUBSTR( str , m [, n ] ) OR SUBSTR( str From m [FOR n] )
 Returns a portion of str, beginning at character m, n chracter long.If
mis 0, it is treated as 1.If m is positive, MySQL counts from the
beginning of str to find the first character. If m is negative, MySQL
counts backwards from the end of str . If n is omitted, MySQL
returns all character to the end of str. If n is less than 1 , a null is
returned.
 Floating point numbers passed as argument to substr are
automatically converted to integer.
Argument type : String,Numeric [,Numeric] Return value : String
5. UPPER/UCASE

 This function converts the given string into upper case.


Syntax
UPPER( str ) OR UCASE( str )

 Returns argument str, with all letters uppercase.


 The return value has the same data type as the argument str.
Argument type : String Return value : String

mysql>SELECT UPPER( 'Large ' ) “Uppercase”;

mysql>SELECT UCASE(' Large' )”Uppercase”;


6. LTRIM

 This function removes leading spaces i.e. spaces from the left
of given string.

Syntax
LTRIM( str )

 Removes soaces from the left of argument str with initial characters
removed.
Argument type : String Return value : String

mysql>SELECT LTRIM( ' RDBMS MySQL ' ) ;


7. RTRIM

 This function removes trailing spaces i.e. spaces from the


right of the given string.

Syntax
RTRIM( str )

 Removes str, with trailing spaces i.e. spaces from the right of given
string.
Argument type : String, Return value : String

mysql>SELECT RTRIM( ' RDBMS MySQL ');


8. TRIM

 This function removes leading and trailing spaces from a


given string.It performs combined functions of LTIM( ) and
RTRIM( )
Syntax
RTRIM( str )
 Returns the string str with all prefixes or suffixes spaces removed.

Argument type : String,Return value : String

mysql>SELECT TRIM( ' Bar One ');


9. INSTR

 This function searches for given second into the given first
string
Syntax
INSTR( str1 , str2 )
 Searches str1 for str2 and returns the position.

Argument type : String, String Return value : Number

mysql>SELECT INSTR( 'CORPORATE FLOOR' , 'OR') AS Instring ;


10. LENGTH

 This function returns the length of a given string in bytes.


Syntax LENGTH ( str1 )
 Returns the length of parameter specified by argument str in
characters.
 If argument str has datatype CHAR, the length includes all trailing
blanks.
 If argument str is null, his function returns null.

Argument type : String Return value : Number

mysql>SELECT INSTR( 'CORPORATE FLOOR' , 'OR') AS Instring ;


11. LEFT

This function returns the leftmost number of characters as


specified
Syntax LEFT( str1 , len)
 Returns the leftmost len characters from the string str.
 Returns NULL if any argument is NULL

Argument type : String, integer Return value : Number


12. RIGHT

This function returns the rightmost number of characters as


specified.
Syntax RIGHT( str1 , len)
 Returns the morightst len characters from the string str.
 Returns NULL if any argument is NULL

Argument type : String, integer Return value : Number


13. MID

This function returns a substring starting from the specified


position.
Syntax MID( str1, pos , len)
 Returns a substring from str strating from pos and having number
of character as len.
 MID (str , pos, len ) is a synonym for SUBSTRING( str , pos ,len ).

Argument type : integer Return value : String


6.2 Numeric Function
The number function are those functions that accepts
numeric values and after performing the required
operation, return numeric values.
Some useful numeric functions are being discussed.
1. MOD
This function returns modulus (i.e. Remainder ) of given two number.
Syntax : MOD(m ,n ) , M % N , M MOD N
 Returns remainder of argument m divided by argument m.
 Returns m if n is 0 i.e. If denominator is 0 .

Argument type : Numeric, Numeric Return Type : Numeric


mysql > SELECT MOD (11 , 4 ) “Modulus” ;
2. POWER / POW
This function returns i.e a number m raised to the nth power.
Syntax : POWER ( m , n) or POW( m , n )
Returns value of argument m raised to the nth power. The base m
and the exponent n can be any numbers , but if m is negative, n
must be an integer.
Argument type : Numeric, Numeric Return Type : Numeric
mysql > SELECT POWER (3 , 2 ) “Raised” ;
3. ROUND
This function returns a number rounded off as per given
specifications.
Syntax : ROUND( n [,m] )
Returns value of argument n rounded to m places right of the
decimal point; if m is omitted to 0 place, m can be negative to
round off digits left of the decimal point. m must be an integer.
Argument type : Numeric, Numeric Return Type : Numeric
mysql > SELECT ROUND ( 15.193 , 1) “Round” ;
4. SIGN
This function returns sign of a given number.
Syntax : SIGN ( n )
 If argument n < 0, the function returns -1;
 If argument n = 0, the function returns 0;
 If argument n > 0, the function returns 1;
Argument type : Numeric Return Type : Numeric
mysql > SELECT SIGN( -15 ) “Sign” ;
5. SQRT
This function returns the square root of given number.
Syntax : SQRT( n )
 Returns square root of argument n. The value n cannot be
negative.
 SQRT returns a “real” result.
Argument type : Numeric Return Type : Numeric
mysql > SELECT SQRT( 26) “Square root” ;
6. TRUNCATE
This function returns a number with some digits truncated.
Syntax : TRUNCATE( n , m )
 Returns value of argument n truccated to argument m decimal
places.
 If argument m is given as 0 places, m can be negative to truncate
(i.e, make zero) m digits left of the decimal point.
Argument type : Numeric, Numeric Return Type : Numeric
mysql > SELECT TRUNCATE( 15.79, 1) “Truncate” ;
6.4 Date/Time Function
Date functions operate on values of
the DATE datatype.
1.CURDATE / CURRENT_DATE ()
This function returns the current date.
Syntax : CURDATE( ) Or CURRENT_DATE( ) Or CURRENT_DATE( )
 Return the current date as a value in 'YYYY-MM-DD' or
'YYYYMMDD' format, depending on whether the function is used
in a string or numeric context.
 CURRENT_DATE and CURRENT_DATE( ) is the synonym for CURDATE( )
Argument type : None Return Type : Date
mysql > SELECT CURDATE( ) ;
2. DATE( )
This function extracts the date part of a date or datetime expression.
Syntax : DATE( expr )
 Extracts the date part of the date or datetime expression
Argument type : date or datetime Return Type : Date
mysql > SELECT DATE( '2008-12-31 01 : 02 :03 ' ) ;
3. MONTH( )
This function extracts the month from the date passed.
Syntax : MONTH( date )
 Returns the month for date, in the range 1 to 12 for January to
December.
 Returns 0 for dates such as '0000-00-00' or '2008-00-00' that have a
zero month part.
Argument type : date Return Type : integer
mysql > SELECT MONTH('2009-02-03' ) ;
4. MONTHNAME( )
This function returns the name of the month for a date.
Syntax : MONTHNAME( date )
 Returns the name pf the month of the given date.
Argument type : date Return Type : string
mysql > SELECT MONTHNAME('2020-05-01' ) ;
5. DAY( )
This function returns the day part of a date
Syntax : DAY( date )
 Returns the day part of the given date.
Argument type : date Return Type : integer
mysql > SELECT DAY('2020-04-13' ) ;
6. YEAR( )
This function returns the year part of a date
Syntax : YEAR( date )
 Returns the year for date. In the range 1000 to 9999.
 Returns 0 for the “zero” date.
Argument type : date Return Type : integer
mysql > SELECT YEAR('2009-02-03' ) ;
7. DAYNAME( )
This function returns the name of weekday.
Syntax : DAYNAME( date )
 Returns the name of the weekday for date.

Argument type : date Return Type : String


mysql > SELECT DAYNAME('2009-02-03' ) ;
8. DAYOFMONTH( )
This function returns the day of month.
Syntax : DAYOFMONTH( date )
 Returns the day of the month for date in the range 1 to 31.
 Returns 0 for dates such as '0000-00-00' or '2008-00-00' .
Argument type : date Return Type : integer
mysql > SELECT DAYOFMONTH('2009-02-03' ) ;
9. DAYOFWEEK( )
This function returns the day of week.
Syntax : DAYOFWEEK( date )
 Returns the weekday index for date ( 1- Sunday, 2-Monday, ... , 7 =
Saturday).
Argument type : date Return Type : integer
mysql > SELECT DAYOFWEEK('2009-02-13' ) ;
10. DAYOFYEAR( )
This function returns the day of the year.
Syntax : DAYOFYEAR( date )
 Returns the day of the year for date , in the range 1 to 366.
Argument type : date Return Type : integer
mysql > SELECT DAYOFYEAR('2009-02-13' ) ;
11. NOW( )
This function returns the current date and time.
Syntax : NOW( )
 Returns the current date and time as a value in 'YYYY-MM-DD
HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether
the function is used in a string or numeric context.
 The value is expressed in the current time zone.
 Now( ) returns a constant time that indicates the time at which the
statment began to execute.
Argument type : none Return Type : datetime
mysql > SELECT NOW() ;

You might also like