structured query language functions

22
STRUCTURED QUERY LANGUAGE FUNCTIONS IN MYSQL

Upload: vineeta-garg

Post on 15-Apr-2017

111 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Structured query language functions

STRUCTURED QUERY LANGUAGEFUNCTIONS IN MYSQL

Page 2: Structured query language functions

FUNCTIONS IN MYSQL

There are two types of functions in MySQL:

Single Row Functions : Single row functions operate on a single value to return a single value. They return only one result per row. They are further categorized into:

Numeric functions String functions Date Time functions

Multiple Row Functions: Multiple row functions operate on a set of rows to return a single value. Examples include SUM(), AVG() and COUNT().

Page 3: Structured query language functions

TABLE: ITEM

Consider the table ITEM:

Itemno Name Price Quantity DOPA001 Bread 20.25 10 2015-05-21A002 Butter 50.75 6 2015-06-12A003 Biscuits 10.32 20 2015-12-23A004 Eggs 70.75 10 2015-09-18A005 Chocolate 100.35 20 2015-10-19

Page 4: Structured query language functions

NUMERIC FUNCIONS

Numeric functions perform operations on numeric values and return numeric values. There are three numeric functions which are available:

POWER(X,Y) OR POW(X,Y): It returns the value of X raised to the power of Y.COMMAND OUTPU

TEXPLANATION

SELECT POW(2,3) ; 8 2 X 2 X 2 =8SELECT POW(2, -2); 0.25 1/22 = ¼ =

0.25

SELECT POW(-2,2); 4 -2 X -2 = 4SELECT POW (-2,3); -8 -2 X -2 X -2 = -8SELECT ITEMNO, POW(PRICE,2) FROM ITEM;

Page 5: Structured query language functions

ROUND FUNCTION ROUND(X,D) function is used to round the value of argument X upto D decimal places. If number of decimal places is not specified or is zero, the number rounds to the nearest

integer OR 0 decimal places. If negative value is specified for precision, it counts off that value left from the decimal point.

If positive value is specified for precision, it counts off that value right from the decimal point.COMMAND RESUL

T EXPLNATION

SELECT ROUND(-4.45); -4 No. after decimal is less than 5SELECT ROUND(-4.67); -5 No. after decimal is more than 5SELECT ROUND(4.45); 4 No. after decimal is less than 5SELECT ROUND(4.678,1); 4.7 No. at second place after decimal is more

than 5SELECT ROUND(4.678,0); 5 No. after decimal is more than 5SELECT ROUND(56.345, -1); 60SELECT ITEMNO, ROUND(PRICE) FROM ITEM;

Page 6: Structured query language functions

TRUNCATE FUNCTION

TRUNCATE(X,D) or TRUNC(X,D) returns the number X, truncated to D decimal places.

If D is 0, the result has no decimal point or fractional part. If D is negative, it causes D digits left of the decimal point of the value X to

become zero.COMMAND OUTPUT

SELECT TRUNC(5.678,0); 5SELECT TRUNC(3.768,1); 3.7SELECT TRUNC(-9.87,1); -9.8SELECT TRUNC(345, -2); 300SELECT ITEMNO,TRUNC(PRICE,1) FROM ITEM;

Page 7: Structured query language functions

STRING(CHARACTER) FUNCTION

String functions operate on character type data. They return either character or numeric values.

LENGTH(str): Returns the length of the string str.

COMMAND OUTPUTSELECT LENGTH(“COMPUTER”); 8SELECT LENGTH(“ I AM LEARNING”); 13SELECT ITEMNO, LENGTH(NAME) FROM ITEM;

Page 8: Structured query language functions

CONCAT FUNCTION

CONCAT(str1, str2,...) : Returns the string that results from concatenating the arguments.

COMMAND OUTPUTSELECT CONCAT(“My”, “SQL”, “CLASS”);

MySQLCLASS

SELECT CONCAT(“My”, “SQL”,NULL, “CLASS”);

NULL

SELECT CONCAT(ITEMNO, NAME), PRICE FROM ITEM;

CONCAT(Itemno, Name)A001BreadA002ButterA003BiscuitsA004EggsA005Chocolate

Page 9: Structured query language functions

INSTR FUNCTION

INSTR(str, substr) : Returns the position of the first occurrence of substring substr in string str

COMMAND RESULT

EXPLANATION

SELECT INSTR(“My SQL”, “SQL);

4 First match found at 4th position

SELECT INSTR(“My SQL”, “SQR”);

0 No match found

SELECT INSTR(NAME, “BREAD”) FROM ITEM;

INSTR(NAME,”BREAD”)10000

Page 10: Structured query language functions

LOWER FUNCTION

LOWER(str) or LCASE(str): Returns the string str in lowercase.

COMMAND RESULTSELECT LOWER(“INFORMATICS”);

informatics

SELECT LOWER(NAME) FROM EMP;

LOWER(Name)breadbutterbiscuitseggschocolate

Page 11: Structured query language functions

UPPER FUNCTION

UPPER(str) or UCASE(str): Returns the string str in lowercase.

COMMAND RESULTSELECT UPPER(“INforMAtiCS”);

INFORMATICS

SELECT UPPER(NAME) FROM EMP;

UPPER(Name)BREADBUTTERBISCUITSEGGSCHOCOLATE

Page 12: Structured query language functions

LEFT FUNCTION

LEFT(str, N) : Returns the N characters from the left side of the string str.

COMMAND RESULTSELECT LEFT(“My SQL”, 2); MySELECT LEFT(NAME, 3) FROM ITEM;

LEFT(Name, 3)BreButBisEggCho

Page 13: Structured query language functions

RIGHT FUNCTION

RIGHT(str, N) : Returns the N characters from the right side of the string str.

COMMAND RESULTSELECT RIGHT(“My SQL”, 3); SQLSELECT RIGHT(NAME, 3) FROM ITEM;

RIGHT(Name)eadteritsggsate

Page 14: Structured query language functions

LTRIM FUNCTION

LTRIM(str) : Returns the leading spaces ie from the left side of the string str.

COMMAND RESULT

SELECT LTRIM(“ INFORMATICS”);

INFORMATICS

SELECT LTRIM(NAME) FROM EMP;

LTRIM(Name)BreadButterBiscuitsEggsChocolate

Page 15: Structured query language functions

RTRIM FUNCTION

RTRIM(str) : Removes the trailing spaces ie from the right side of the string str.

COMMAND RESULTSELECT RTRIM(“INFORMATICS ”);

INFORMATICS

SELECT RTRIM(NAME) FROM EMP;

RTRIM(Name)BreadButterBiscuitsEggsChocolate

Page 16: Structured query language functions

TRIM FUNCTION

TRIM(str) : Removes both leading and trailing spaces from the string str .

COMMAND OUTPUTSELECT TRIM(“ INFORMATICS PRACTICES ”);

INFORMATICS

SELECT TRIM(NAME) FROM EMP;

TRIM(Name)BreadButterBiscuitsEggsChocolate

Page 17: Structured query language functions

SUBSTRING/SUBSTR/MID FUNCTION

SUBSTRING(str, M,N) or MID(str, M,N): Returns the specified number of characters from the middle of the string. There are 3 arguments.

The first argument is the source string. The second argument is the position of first character to be displayed. The third argument is the number of characters to be displayed.

If the third argument is missing, then starting from the position specified, the rest of the string is returned.

If the second argument is negative, then the beginning of the substring is M characters from the end of the string.

Page 18: Structured query language functions

SUBSTRING/SUBSTR/MID FUNCTION Contd…

COMMAND OUTPUTSELECT SUBSTR(“WORLD OF COMPUTERS”, 4 ); orSELECT SUBSTR(“WORLD OF COMPUTERS” FROM 4 );

LD OF COMPUTERS

SELECT SUBSTR(“WORLD OF COMPUTERS” , 3, 4 );

LD O

SELECT SUBSTR(“WORLD OF COMPUTERS”, -4 ); TERSSELECT SUBSTR(“WORLD OF COMPUTERS”, -6,3 );

PUT

SELECT SUBSTR(“WORLD OF COMPUTERS” FROM -4 FOR 2 );

TE

SELECT SUBSTR(NAME,2,3) FROM ITEM;

SUBSTR(Name,2,3)reauttiscggshoc

Page 19: Structured query language functions

ASCII FUNCTION

ASCII(str): Returns the ASCII value of the leftmost character of the string str. Returns 0 if str is an empty string. Returns NULL if str is NULL.

COMMAND RESULT

EXPLANATION

SELECT ASCII(‘2’); 50 ASCII value of ‘0’ is 48, ‘1’ is 49 , ‘2’ is 50 and so on

SELECT ASCII(‘CG’);

67 ASCII value of ‘A’ is 65, ‘B’ is 66, ‘C’ is 67 and so on

SELECT ASCII(‘are’);

97 ASCII value of ‘a’ is 97, ‘b’ is 98 and so on

Page 20: Structured query language functions

DATE AND TIME FUNCTION

COMMAND EXPLANATION EXAMPLE RESULTCURDATE() Returns the current date in YYYY-

MM-DD formatSELECT CURDATE(); 2015-10-14

NOW() Returns the current date and time in 'YYYY-MM-DD HH:MM:SS'

SELECT NOW(); 2015-10-14 20:10:30

SYSDATE() Returns the current date and time in 'YYYY-MM-DD HH:MM:SS

SELECT SYSDATE(); 2015-10-14 20:10:30

DATE(exp) Extracts the date part of a date or date time expression

SELECT DATE(‘2015-10-14 20:10:30’);

2015-10-14

MONTH(date) Returns the numeric month from the date passed, in the range 0 to 12

SELECT MONTH(‘2015-10-14’);SELECT MONTH(DOP) FROM ITEM;

10

Page 21: Structured query language functions

DATE AND TIME FUNCTION Contd……

COMMAND EXPLANATION EXAMPLE RESULTYEAR(date) Returns the year for date passed

in the range 0 to 9999SELECT YEAR (‘2015-10-14’);SELECT YEAR(DAP) FROM ITEM;

2015

DAYNAME(date)

returns the name of the weekday for the date passed

SELECT DAYNAME(2015-10-14); SELECT DAYNAME(DAP) FROM ITEM;

MONDAY

DAYOFMONTH(date)

Returns the day of the month in the range 0 to 31.

SELECT DAYOFMONTH(2015-10-14); SELECT DAYOFMONTH(DAP) FROM ITEM;

DAYOFWEEK(date)

Returns the day of week in number as 1 for Sunday, 2 for Monday and so on.

SELECT DAYOFWEEK(2015-10-14);SELECT DAYOFWEEK(DAP) FROM ITEM;

DAYOFYEAR(date)

Return the day of the year for the given date in numeric format in the range 1 to 366.

SELECT DAYOFYEAR(2015-10-14);SELECT DAYOFYEAR(DAP) FROM ITEM;

Page 22: Structured query language functions

SYSDATE() vs NOW()

SYSDATE() returns the time at which the function executes. While NOW() which returns a constant time that indicates the time at which

the statement began to execute.