9.SQL Queries-single Row Functions to Custmoized Output
The document provides an overview of single row functions in Oracle SQL, which operate on individual rows and return a single output per row. It categorizes these functions into numeric, character, conversion, and date functions, detailing various examples and their usage. Additionally, it distinguishes between single row functions and multiple row functions, emphasizing the specific capabilities of scalar functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views
9.SQL Queries-single Row Functions to Custmoized Output
The document provides an overview of single row functions in Oracle SQL, which operate on individual rows and return a single output per row. It categorizes these functions into numeric, character, conversion, and date functions, detailing various examples and their usage. Additionally, it distinguishes between single row functions and multiple row functions, emphasizing the specific capabilities of scalar functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22
SQL QUERIES-6
SINGLE ROW FUNCTIONS
FUNCTIONS • Oracle SQL supplies a rich library of in-built functions which can be employed for various tasks. • The essential capabilities of a functions can be the case conversion of strings, in-string or substring operations, mathematical computations on numeric data, and date operations on date type values. • SQL Functions optionally take arguments from the user and mandatorily return a value. • On a broader category, there are 2 types of functions :- I. Single Row functions - Single row functions are the one who work on single row and return one output per row. row For example, length and case conversion functions are single row functions. II.Multiple Row functions - Multiple row functions work upon group of rows and return one result for the complete set of rows. rows They are also known as Group Functions. Functions Single row functions(SCALAR )
• They act on only 1 value at a time whereas aggregate
functions act on a set of values. But both of them (SCALAR & aggregate functions)produce a single value as an output. • Scalar functions are classified according to several data types. a) Numeric functions b)Character functions c) Date functions d)Conversion functions 1. Numeric Functions
• Numeric functions accept numeric input and return
numeric values. values Most numeric functions return NUMBER values that are accurate to 38 decimal digits. • The transcendental functions COS, COSH, EXP, LN, LOG, SIN, SINH, SQRT, TAN, and TANH are accurate to 36 decimal digits. • The transcendental functions ACOS, ASIN, ATAN, and ATAN2 are accurate to 30 decimal digits. The numeric functions are: 1.ROUND(n1,n2)- It returns n1 rounded to n2 places to right of decimal point. Here n1 is a number and n2 must be an integer. integer SELECT ROUND(12.28,1) from dual; 2. Trunc(n1,n2)- it returns n1 truncated to n2 digits of precision. Here n1 is number, number n2 must be integer. integer SELECT trunc(12.218,2) from dual; 3. Mod(n1,n2)- It returns remainder of n1 divided by n2. n2 SELECT mod(9,2) from dual; 4. ABS(n)- It returns absolute value of n. n Here n is a number. SELECT ABS(-20) from dual; 5. POWER(n1,n2)- It returns n1 raised to n2 power. power SELECT POWER(4,3) from dual; 6. sqrt(n)- returns square root SELECT sqrt(16) from dual; 7. exp(n)- It returns e raised to nth power. power Here e is a constant=2.71828123 SELECT EXP(1) from dual; 8. Greatest(n1,n2,...nn)- It returns greatest value. value SELECT greatest(10,13,11) from dual; 9. Least(n1,n2,...nn)- It returns least value. value SELECT least(10,13,11) from dual;
10. floor(n)- SELECT floor(13.9) from dual;
dual
11.ceil(n)- SELECT ceil(10.27) from dual;
2. Character functions
• Accepts character input and returns number or character
value. value • Functions under the category are CONCAT, LENGTH, SUBSTR, INSTR, LPAD, RPAD, TRIM and REPLACE. • Case Conversion functions - Accepts character input and returns a character value. value Functions under the category are UPPER, LOWER and INITCAP. INITCAP 1. length(string)- It returns number of characters in a given string. SELECT length('computer') from dual;
2. substr(string,n1,n2)- n1 and n2 will be integers. It
returns a sub string that begins at n1th character and length of that substring is n2. SELECT substr('computer',4,3) from dual; dual
3. lower: converts string in lower case
SELECT lower('HAPPY') from dual;
4. upper: converts string in uppercase
SELECT upper('happy') from dual; 5. initcap(string)- It returns string with first character in uppercase. uppercase SELECT initcap('happy') from dual; 6. ascii(character)- it returns ASCII value of a given character. SELECT ascii('B') from dual; 7.translate(s1,s2,s3)- replace s1 with s3, s3 wherever s2 is coming. SELECT translate('computer','mr','ns') from dual; 8. ltrim(string, char)- it returns the string after removing leading char(it removes the spaces before leading character) SELECT ltrim(‘ ananya') from dual; SELECT length(ltrim(‘ ananya’)) from dual; 9. rtrim(string, char)- it returns the string after removing trailing char SELECT rtrim('ananya') from dual; 10. trim(string)- it returns the string after removing all the blank spaces from left and right sides of string. SELECT trim(' ananya ') from dual 11. lpad(string,n,char)- it returns the string after padding char to left of string. string The returning string becomes n characters long. SELECT lpad('hello',8,'?') from dual; 12. rpad(string,n,char)- it returns the string after padding char to right of string. string The returning string becomes n characters long. SELECT rpad('hello',8,'?') from dual; 13.replace(s1,s2,s3) SELECT replace('computer','put','son') from dual; 14. CONCAT returns char1 concatenated with char2. char2 Both char1 and char2 can be any of the data types The string returned is in the same character set as char1. Its data type depends on the data types of the arguments. SELECT concat(name,city) from student where id=1101; SELECT concat(concat(name,’ LIVES in ‘),city) from student where id=1101; 3. CONVERSION FUNCTIONS 1. TO_CHAR(number/date,format): • it converts number/date datatype to character data type. SELECT to_char(2000,'$99999') from dual; SELECT to_char(sysdate,'DD-MM-YYYY') from dual; SELECT to_char(sysdate.’day’-’month’-’year’) from dual; 2. TO_NUMBER(char): • It converts a value of character data type to a number data type. SELECT to_number('567') from dual; 3. TO_DATE(character,format): • It converts character value to a date value as per format. SELECT to_date('17/10/09','DD-MM-YY') from dual; 4. DATE FUNCTIONS • YYYY: Four digit representation of year • YEAR: Year spelled out • MM: Two digit value of month • MONTH: Full name of month • MON: Three letter representation of month • DY: Three letter representation of the day of the week • DAY: Full name of the day • DD: Numeric day of the month • fm: used to remove any padded blanks or leading zeros. 1. sysdate- Returns the current date-time from the operating system of the database server.
SELECT sysdate from dual;
SELECT systimestamp from dual;
2. current_date- Similar to the sysdate function, but
returns the current date-time within the sessions time zone. SELECT current_date from dual; SELECT current_timestamp from dual; 3. last_day(date)- it returns the last date of the month for date specified with the function. SELECT sysdate,last_day(sysdate) from dual; If sysdate =17-oct-2016, then last_day=31-oct-2016 4. next_day(date,day)- it returns the date for first day after the date specified in the function SELECT next_day(sysdate,'wednesday') from dual; 5. months_between(dat1,date2)- It returns number of months between date1 and date2. date2 SELECT months_between('21-oct-09','21-sep-09') from dual; 6. add_months(date,n)- here n is integer value. It returns the date after adding number of months(n) months in the function. SELECT add_months('17-oct-09',2) from dual; //answer is 17-dec-09 7. round(date,format)- It returns the date after rounding to a specific format. SELECT round(sysdate,'yy') from dual 8. TRUNC(date, [format])- Truncates the specified date of its time portion according to the format provided. SELECT trunc(to_date('27-oct-92'), 'year') as new_year from dual; 9. to_date- to calculate number of days between dates. dates SELECT (to_date ('15-jun-13') - to_date('01-feb-12')) from dual; SELECT (to_date ('15-06-13','dd-MM-yyyy') - to_date('01- 02-12','dd-MM-yyyy')) from dual • When used with dates, ROUND and TRUNC functions, you can round dates to nearest year or month. • If the format model is month , dates 1-15 result in the first day of the current month. • dates 16-31 result in the first day of the next month. • if the format model is year , months 1-6 result in january 1 of the current year. • months 7-12 result in January 1 of the next year. • select extract(day from sysdate) as only_day from dual • select extract(month from sysdate) as only_month from dual • select extract(year from sysdate) as only_year from dual