You are on page 1of 4

My – SQL Functions

String Functions: The string functions in MySQL can manipulate the


text string in many ways.
1. CHAR(): This function returns the character for each integer passed.
Syntax: CHAR( value-1, value-2)
Example :
SELECT CHAR(65,66,67,68);

Syntax: CONCAT ( str1, str2,…)

Example : SELECT CONCAT (‘INFO.’, ‘PRACTICES’);

3. LOWER / LCASE: This functions converts a string into lowercase.


Syntax: lower / lcase (string)
Example : SELECT LCASE (‘COMPUTER’);

4. UPPER / UCASE : This functions converts the given string into upper
case.
Syntax : upper / ucase (string)
Example : SELECT UPPER(‘computer’);

5. TRIM : This functions removes leading and trailing spaces from a


given string.
Example : SELECT TRIM (‘ COMPUTER ‘);

6. INSTR : This function searches for given second string into the given
first string.
Syntax : INSTR(string-1, string-2)
Example: SELECT INSTR(‘CORPORATE FLOOR’,’OR’);
-1-
7. LENGTH : This functions returns the length of a given string.

Syntax : LENGTH (String)


Example : SELECT LENGTH (‘COMPUTER’);

8. LEFT : This functions returns the leftmost number of characters as


specified.
Syntax: LEFT (string, no. of characters)
Example : SELECT LEFT (‘COMPUTER’,4);

9. RIGHT : This functions returns the rightmost number of characters as


specified.
Syntax: RIGHT (string, no. of characters)
Example : SELECT RIGHT ( ‘COMPUTER’,4);

10. SUBSTR : This function extracts a substring from a given string.

Example : SELECT SUBSTR(‘ABCDEF’,2,3);

Numeric Functions : The number functions are those functions that


accepts numeric values and after performing the required operations,
return numeric values.
1. MOD : This function returns modulus ( i.e. remainder) of given
numbers.
Syntax : SELECT MOD (m,n);
Example : SELECT MOD (14,3);

2. POWER / POW : This functions returns m raised to nth power.

Example :SELECT POWER (3,2);


-2-
3. SQRT : This functions returns the square root of given number.

Example : SELECT SQRT (45);

4. SIGN : This function returns sign of given number.

If number is -ive output is value -1


If number is 0 output is zero
If number is +ive output is 1

Example : SELECT SIGN (-20);

5. ROUND : This function returns a number rounded off as per given


specifications.
Example : SELECT ROUND (145.345,2);

6. TRUNCATE : This returns a number with some digits truncated.


Example : SELECT TRUNCATE (15.56,1);

DATE & TIME FUNCTIONS:

1. CURDATE () : This function returns the current date.


Example : SELECT CURDATE();

2. MONTH(): This function returns the month from the date passed.
Example : SELECT MONTH(‘2021-07-11’);
3. MONTHNAME(): This function returns the name of the month for a
date.
Example : SELECT MONTHNAME(‘2021-07-11’);
-3-
4. DAY() : This function returns the day part of date.
Example : SELECT DAY(‘2021-07-11’);

5.YEAR(): This function returns the year part of a date.


Example : SELECT YEAR(‘2021-07-11’);

6. DAYNAME() : This function returns the name of weekday.


Example : SELECT DAYNAME(‘2021-07-11’);

7. DAYOFMONTH() : This function returns the day of month.


Example : SELECT DAYOFMONTH(‘2021-07-11’);

8. DAYOFWEEK(): This function returns the day of week.


Example : SELECT DAYOFWEEK(‘2021-07-11’);

9. DAYOFYEAR(): This function returns the day of the year.


Example : SELECT DAYOFYEAR(‘2021-07-11’);

10.NOW(): This function returns the current date and time.


Example: SELECT NOW();

-4-

You might also like