Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open Source Server Side Scripting MySQL Functions

Similar presentations


Presentation on theme: "Open Source Server Side Scripting MySQL Functions"— Presentation transcript:

1 Open Source Server Side Scripting MySQL Functions
ECA 236 Open Source Server Side Scripting MySQL Functions Open Source Server Side Scripting

2 Open Source Server Side Scripting
MySQL functions MySQL has many functions available to alter or format the data stored in tables when using a MySQL function specify a column name case insensitive no space between function name and parentheses SELECT FUNCTION( column_name ) FROM table_name; SELECT column1, FUNCTION( column2 ), column3 FROM table_name; ECA 236 Open Source Server Side Scripting

3 Open Source Server Side Scripting
MySQL text functions Function Usage Purpose CONCAT( ) CONCAT( x, x, … ) Creates a new string of the form xy CONCAT_WS( ) CONCAT( separator, x, y, … ) Creates a new string, with the separator between each string CONV( ) CONV( n, from_base, to_base ) Converts numbers between different number bases LEFT( ) LEFT( column, x ) Returns the leftmost x characters from a column’s value RIGHT( ) RIGHT( column, x ) Returns the rightmost x characters from a column’s value TRIM( ) TRIM( column ) Trims excess space from the beginning and end of the stored value ECA 236 Open Source Server Side Scripting

4 MySQL text functions cont …
Usage Purpose UPPER( ) UPPER( column ) Capitalizes the stored string LOWER( ) LOWER( column ) Turns the stored string into all lowercase LPAD( ) LPAD( column, length, pad_character Returns the string, left-padded with the designated character, to a designated length RPAD( ) RPAD( column, length, pad_character Returns the string, right-padded with the designated character, to a designated length SUBSTRING( ) SUBSTRING( column, start, length ) Returns length characters from column beginning with start ( indexed from 0 ) LENGTH( ) LENGTH( column ) Returns the length of the value stored in the column ECA 236 Open Source Server Side Scripting

5 MySQL text functions cont …
CONCAT( ) concatenates arguments no default separator CONCAT_WS( ) CONCAT With Separator first argument is designated separator SELECT CONCAT( last_name, ‘, ‘, first_name ) FROM users; SELECT CONCAT_WS( ‘ ‘, first_name, last_name) FROM users ORDER BY last_name; ECA 236 Open Source Server Side Scripting

6 Open Source Server Side Scripting
alias alias temporary renaming of table or column – allows for quick reference used with keyword AS case sensitive once defined, query must use alias SELECT CONCAT(last_name, ', ', first_name) AS fn FROM users ORDER BY fn; ECA 236 Open Source Server Side Scripting

7 MySQL text functions cont …
CONV( ) converts numbers between different number bases 3 arguments number to convert base to convert from base to convert to SELECT CONV( ‘ffffff’, 16, 10 ); ECA 236 Open Source Server Side Scripting

8 MySQL text functions cont …
LEFT( ) returns the designated number of characters from the left of the value RIGHT( ) returns the designated number of characters from the right of the value SELECT LEFT( last_name, 3 ) FROM users ORDER BY last_name; SELECT RIGHT( , 4 ) FROM users; ECA 236 Open Source Server Side Scripting

9 MySQL text functions cont …
TRIM( ) strips all white space from the beginning and end of the value similar functions include LTRIM( ) and RTRIM( ) to trim the white space from only beginning or end TRIM( ) can also be used to trim specified prefixes or suffixes SELECT TRIM( ) FROM users; SELECT TRIM( LEADING FROM ); SELECT TRIM( TRAILING ‘room’ FROM ‘mushroom’ ); ECA 236 Open Source Server Side Scripting

10 MySQL text functions cont …
UPPER( ) converts the value to uppercase LOWER( ) converts the value to lowercase SELECT UPPER( last_name ) FROM users; SELECT LOWER( last_name ) FROM users; ECA 236 Open Source Server Side Scripting

11 MySQL text functions cont …
LPAD( ) returns the value, left padded with the designated string, the designated number of characters RPAD( ) returns the value, right padded with the designated string, the designated number of characters SELECT LPAD( last_name, 16, ‘*’ ) FROM users; SELECT RPAD( last_name, 16, ‘*’ ) FROM users; ECA 236 Open Source Server Side Scripting

12 MySQL text functions cont …
SUBSTRING( ) returns a substring of the value, the designated number of characters long, beginning at the designated position LENGTH( ) Returns the length of the column value SELECT SUBSTRING( last_name, 1, 3 ) AS ln FROM users ORDER BY ln; SELECT LENGTH( first_name ), LENGTH( last_name ) FROM users ORDER BY last_name; ECA 236 Open Source Server Side Scripting

13 MySQL numeric functions
Usage Purpose ABS( ) ABS( x ) Returns the absolute value of x CEILING( ) CEILING( x ) Returns the next highest integer based on the value of x FLOOR( ) FLOOR( x ) Returns the integer value of x FORMAT( ) FORMAT( x, d ) Returns x formatted as a number with d decimal places, and commas every three spaces MOD( ) MOD( x, y ) Returns modulus of dividing x by y RAND( ) Returns a random number between 0 and 1.0 ECA 236 Open Source Server Side Scripting

14 MySQL numeric functions cont …
Usage Purpose ROUND( ) ROUND( x, d ) Returns the number x rounded to d decimal places SIGN( ) SIGN( x ) Returns the sign of a x SQRT( ) SQRT( x ) Calculates the square root of x TRUNCATE( ) TRUNCATE( x, d ) Returns the number x, truncated to d decimals LEAST( ) LEAST( x, y, … ) Passed 2 or more arguments, it returns the smallest GREATEST( ) GREATEST( x, y, … ) Passed 2 or more arguments, it returns the largest ECA 236 Open Source Server Side Scripting

15 MySQL numeric functions cont …
ABS( ) returns the absolute value CEILING( ) returns the next highest integer, based on the value SELECT ABS( -32 ); SELECT CEILING( ); ECA 236 Open Source Server Side Scripting

16 MySQL numeric functions cont …
FLOOR( ) returns the integer value of a number FORMAT( ) returns a number formatted with the designated number of decimal places, and commas every three spaces SELECT FLOOR( ); SELECT FORMAT( , 2 ); ECA 236 Open Source Server Side Scripting

17 MySQL numeric functions cont …
MOD( ) returns modulus RAND( ) returns a random number between 0 and 1.0 SELECT MOD( 7, 4 ); SELECT RAND( ); ECA 236 Open Source Server Side Scripting

18 MySQL numeric functions cont …
ROUND( ) returns the number rounded to the designated decimal places SIGN( ) returns a –1, 0, or 1 depending upon whether the number is negative, zero, or positive SELECT ROUND( , 2 ); SELECT SIGN( -47 ); SELECT SIGN( 0 ); SELECT SIGN( 29 ); ECA 236 Open Source Server Side Scripting

19 MySQL numeric functions cont …
SQRT( ) calculates the square root of a number TRUNCATE( ) returns a number, truncated to the designated number of decimals SELECT SQRT( 64 ); SELECT TRUNCATE( , 2 ); ECA 236 Open Source Server Side Scripting

20 MySQL numeric functions cont …
LEAST( ) returns the smallest of at least 2 supplied arguments GREATEST( ) returns the largest of at least 2 supplied arguments SELECT LEAST( 2, 13, 8, 9); SELECT GREATEST( 2, 13, 8, 9); ECA 236 Open Source Server Side Scripting

21 MySQL date/time functions
Usage Purpose DATE( ) DATE( x ) Extracts the date part of a value TIME( ) TIME( x ) Extracts the time part of a value HOUR( ) HOUR( x ) Returns the hour of a stored value MINUTE( ) MINUTE( x ) Returns the minute of a stored value SECOND( ) SECOND( x ) Returns the second of a stored value DAYNAME( ) DAYNAME( x ) Returns the name of the day of a stored value DAYOFMONTH( ) DAYOFMONTH( x ) Returns the numerical day value of a stored value MONTHNAME( ) MONTHNAME( x ) Returns the name of the month of a stored value ECA 236 Open Source Server Side Scripting

22 MySQL date/time functions cont …
Usage Purpose MONTH( ) MONTH( x ) Returns the numerical month value of a stored value YEAR( ) YEAR( x ) Returns the year of a stored value ADDDATE( ) ADDDATE( x, INTERVAL t type ) Returns the value of x units added to the stored value SUBDATE( ) SUBDATE( x, INTERVAL t type ) Returns the value of x units subtracted from the stored value CURDATE( ) Returns the current date CURTIME( ) Returns the current time NOW( ) Returns the current date and time UNIX_TIMESTAMP( ) UNIX_TIMESTAMP(date) Returns the number of seconds since the Epoch ECA 236 Open Source Server Side Scripting

23 MySQL date/time functions cont …
extracts the date part of a DATE or DATETIME expression available in MySQL version 4.1.1 TIME( ) extracts the time part of a TIME or DATETIME expression SELECT DATE( registration_date ) FROM users; SELECT TIME( registration_date ) FROM users; ECA 236 Open Source Server Side Scripting

24 MySQL date/time functions cont …
HOUR( ) returns the hour of a value, in the range 0 – 23 MINUTE( ) returns the minute of a value, in the range 0 – 59 SECOND( ) returns the second of a value, in the range 0 – 59 SELECT HOUR( registration_date ) FROM users; SELECT MINUTE( registration_date ) FROM users; SELECT SECOND( registration_date ) FROM users; ECA 236 Open Source Server Side Scripting

25 MySQL date/time functions cont …
DAYNAME( ) returns the name of the weekday of a value DAYOFMONTH( ) returns the day of the month of a value, in the range 1 – 31 MONTHNAME( ) returns the name of the month of a value SELECT DAYNAME( registration_date ) FROM users; SELECT DAYOFMONTH( registration_date ) FROM users; SELECT MONTHNAME( registration_date ) FROM users; ECA 236 Open Source Server Side Scripting

26 MySQL date/time functions cont …
MONTH( ) returns the month of a value, in the range 1 – 12 YEAR( ) returns the year of a value in the range 1000 – 9999 SELECT MONTH( registration_date ) FROM users; SELECT YEAR( registration_date ) FROM users; ECA 236 Open Source Server Side Scripting

27 MySQL date/time functions cont …
ADDDATE( ) used to perform calculations on date values synonymous with DATE_ADD( ) prototype : returns the value of date after x units of type have been added ADDDATE( date, INTERVAL x type ) ECA 236 Open Source Server Side Scripting

28 MySQL date/time functions cont …
ADDDATE( ) types include ADDDATE( ) type values SECOND YEAR MINUTE MINUTE_SECOND HOUR HOUR_MINUTE DAY DAY_HOUR MONTH YEAR_MONTH ECA 236 Open Source Server Side Scripting

29 MySQL date/time functions cont …
ADDDATE( ) add 2 hours to a date/time value add 3 weeks to a date/time value add 18 months to a date/time value SELECT ADDDATE( registration_date, INTERVAL 2 HOUR ) FROM users; SELECT ADDDATE( registration_date, INTERVAL 21 DAY ) FROM users; SELECT ADDDATE( registration_date, INTERVAL ‘1-6’ YEAR_MONTH ) FROM users; ECA 236 Open Source Server Side Scripting

30 MySQL date/time functions cont …
SUBDATE( ) similar to ADDDATE( ), but subtracts from value CURDATE( ) returns the current date CURTIME( ) returns the current time SELECT SUBDATE( registration_date, INTERVAL 1 DAY ) FROM users; SELECT CURDATE( ); SELECT CURTIME( ); ECA 236 Open Source Server Side Scripting

31 MySQL date/time functions cont …
NOW( ) returns the current date and time UNIX_TIMESTAMP( ) returns the number of seconds since the Unix Epoch SELECT NOW( ); SELECT UNIX_TIMESTAMP( ‘ :46:40’ ); ECA 236 Open Source Server Side Scripting


Download ppt "Open Source Server Side Scripting MySQL Functions"

Similar presentations


Ads by Google