Page 1

SQL FUNCTIONS 1 Introduction 1.1 SQL Aggregate Functions 1.2 SQL Scalar Functions 1.2.1 LOWER 1.2.2 UPPER 1.2.3 SUBSTR 1.2.4 CONCAT 1.2.5 REPLACE 1.2.6 LENGTH 1.2.7 TRIM 1.2.8 LTRIM 1.2.9 RTRIM 1.2.10 ROUND 1.2.11 FLOOR 1.2.12 SYSDATE 1.2.13 ADD_MONTH 1.2.14 LAST_DAY 1.2.15 MONTHS_BETWEEN 1.2.16 Next_DAY 1.2.17 TO_CHAR 1.2.18 TO_DATE

Contents

1.Introduction SQL has many built-in functions for performing calculations on data. There are two main Categories of SQL Functions:

1.1 SQL Aggregate Functions SQL aggregate functions return a single value, calculated from values in a column in table. Some Aggregate Functions are:     

AVG() - Returns the average value COUNT() - Returns the number of rows MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum value

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2 SQL Scalar Functions SQL scalar functions return a single value, based on the input value. Note: These scalar functions may have different names & syntax in different DBMS packages. So developers have to refer the respective DBMS manual for more information about functions. Here we will discuss some Scalar Functions based on Oracle DBMS package.

1.2.1 LOWER It returns the char, with all letters to lower case.

SELECT LOWER('INDIA') FROM DUAL; Query Result: LOWER India

1.2.2 UPPER It returns the char, with all letters to upper case.

SELECT UPPER('india') FROM DUAL; Query Result: UPPER INDIA

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2.3 SUBSTR It returns the substring from char.

SELECT SUBSTR('Oxygen',3,4) FROM DUAL; Query Result: SUBS ygen

1.2.4 CONCAT It converts the two strings into a single string.

SELECT CONCAT('STR','ING') FROM DUAL; Query Result: CONCAT STRING

1.2.5 REPLACE It replaces one char with another.

SELECT REPLACE('POLY','STER') "REPLACE" FROM DUAL; Query Result: REPL POLY

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2.6 LENGTH It returns the length (numeric) of a given char.

SELECT LENGTH('POLYSTER') "LEN" FROM DUAL; Query Result: LEN 8

1.2.7 TRIM Removes all specified characters either from beginning or ending of a string, By default it removes white spaces from both ends of string.

SELECT TRIM(BOTH,'a' FROM 'australia') "TRIM" FROM DUAL; Query Result: TRIM ustrali

1.2.8 LTRIM It removes specified characters from left side of a string. By default it removes white space from left of a string.

SELECT LTRIM('australia','a') "LTRIM" FROM DUAL; Query Result: LTRIM ustralia

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2.9 RTRIM It removes specified characters from right side of a string. By default it removes white space from right of a string.

SELECT RTRIM('obelisk','k') "RTRIM" FROM DUAL; Query Result: RTRIM obelis

1.2.10 ROUND It returns n rounded to integer places to the right of the decimal point.

SELECT ROUND(14.183,1) "ROUND" FROM DUAL; Query Result: ROUND 14.2

1.2.11 FLOOR It returns n rounded to integer places to the right of the decimal point.

SELECT FLOOR(13.7) "FLOOR" FROM DUAL; Query Result: FLOOR 13

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2.12 SYSDATE It returns the current date / time from Oracle system time.

SELECT SYSDATE FROM DUAL; Query Result: SYSDATE 01-JAN-11

1.2.13 ADD_MONTH It returns date after adding number of months to the specified date.

SELECT ADD_MONTHS(SYSDATE,2)"Month Add" FROM DUAL; Query Result: SYSDATE 01-MAR-11

1.2.14 LAST_DAY It returns the last date of month specified with the function.

SELECT SYSDATE,LAST_DAY(SYSDATE) FROM DUAL; Query Result: SYSDATE LAST_DAY 01-JAN-11 31-JAN-11

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2.15 MONTHS_BETWEEN It returns the number Of month between two dates.

SELECT MONTHS_BETWEEN('01-MAR-2011',SYSDATE) "Months_Between" FROM DUAL; Query Result: Months_Between 2

1.2.16 Next_DAY It returns the date of the upcoming week day named by char parameter followed by date parameter.

SELECT NEXT_DAY(SYSDATE,'WEDNESDAY') "NXT UP WED" FROM DUAL; Query Result: NXT UP WED 5-JAN-11

1.2.17 TO_CHAR It retrieves the date in a format different from the default format.

SELECT TO_CHAR(SYSDATE,'DD-MM-YYYY') "UK DATE" FROM DUAL; Query Result: UK DATE 01-01-2011 Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS 1.2.18 TO_DATE It converts the char value to date value.

SELECT TO_DATE('2/1/2011','DD/MM/YY') "ORCL FORMAT" FROM DUAL; Query Result: ORCL FORMAT 02-JAN-11

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


SQL FUNCTIONS Get Free Computer Science Study Kit Android App From

OR

For more lecture notes and Computer Science Study Kit Android app visit us at http://computer-science-study-kit.blogspot.in/ We will be happy to have your comments and ideas about these notes and app also and if you find these notes helpful, please do share them with your friends and colleagues.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/

Sql Functions  

SQL has many built-in functions for performing calculations on data. There are two main Categories of SQL Functions: 1) Aggregate Function...

Read more
Read more
Similar to
Popular now
Just for you