Open Source Server Side Scripting MySQL Functions

Slides:



Advertisements
Similar presentations
N.G.Acharya & D.K.Marathe college Chembur-E, Mumbai-71
Advertisements

Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Using Single-Row Functions to Customize Output
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
Number Functions. 2 home back first prev next last Review single-row character functions –character case-manipulation functions  LOWER, UPPER, INITCAP.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
SINGLE-ROW FUNCTIONS Lecture 9. SQL Functions Functions are very powerful feature of SQL and can be used to do the following:  Perform a calculation.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
After completing this lesson, you should be able to do the following: Describe various types of functions available in MySQL Use character, number, and.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
A Guide to SQL, Seventh Edition. Objectives Understand how to use functions in queries Use the UPPER and LOWER functions with character data Use the ROUND.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Slide 1 PHP Predefined Functions ITWA113. Murach’s ASP.NET 3.5/C#, C1© 2008, Mike Murach & Associates, Inc. Slide 2 PHP Predefined Functions Objectives.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
1 Inside Module 8 Extracting Data Page n Using the Extract command2 n Coercion3 n $-functions4 n Extract from a table7.
[ ] Square brackets enclose syntax options { } Braces enclose items of which only one is required | A vertical bar denotes options … Three dots indicate.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
A Guide to SQL, Seventh Edition
MYSQL DEFINITION MySQL, pronounced either "My S-Q-L" or "My Sequel," is an open source relational database management system. It is based on the structure.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Open Source Server Side Scripting Permissions & Users
LOCO Extract – Transform - Load
Built-in Functions. Usage of Wildcards
SQL DATE/TIME FUNCTIONS
SQL 101 3rd Session.
Writing Basic SQL SELECT Statements
Computations Done on table data
Using Single-Row Functions to Customize Output
SQL Text Manipulation Farrokh Alemi, Ph.D.
CS122 Using Relational Databases and SQL
CSI 101 Elements of Computing Spring 2009
Single-Row Functions Lecture 9.
Writing Basic SQL SELECT Statements
VBScript Session 10.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
SQL Dates FdSc in ICT.
Inside Module 8 Extracting Data Page Using the Extract command 2
Lecture 5 SQL FUNCTIONS.
Trainer: Bach Ngoc Toan– TEDU Website:
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

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

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

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

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

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

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

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

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( email, 4 ) FROM users; ECA 236 Open Source Server Side Scripting

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( email ) FROM users; SELECT TRIM( LEADING ‘@’ FROM ‘@@@mushroom’ ); SELECT TRIM( TRAILING ‘room’ FROM ‘mushroom’ ); ECA 236 Open Source Server Side Scripting

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

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

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

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

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

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

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( 3.0000004 ); SELECT FORMAT( 369635, 2 ); ECA 236 Open Source Server Side Scripting

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

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( 3.127458, 2 ); SELECT SIGN( -47 ); SELECT SIGN( 0 ); SELECT SIGN( 29 ); ECA 236 Open Source Server Side Scripting

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( 3.127458, 2 ); ECA 236 Open Source Server Side Scripting

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

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

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

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

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

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

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

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

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

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

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

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( ‘2001-09-08 21:46:40’ ); ECA 236 Open Source Server Side Scripting