Thursday, February 27, 2014

Different types Functions in Oracle with examples

Hi All,

In this post we will discuss about several functions used in oracle

select INSTR('MAHESH', 'H',-1,1) from dual;-- Output  :6

select RTRIM('MAHESMMM', 'M') from dual;--MAHES

select replace('mahesh', 'h','e') from dual;--maeese

select translate('Led','Le','R') from dual;--Translates letter by letter. In this example, L is replaced by R, but e is replaced but with nothing. Output : Rd

select chr(68), ASCII('D'), ASCII('MAHESH'), ASCII('M') from dual;--Output : D 68 77 77

select round(123.123456789, -2) from dual;--Output : 100

select 19.637, trunc(19.637, -1), round(19.637, -1) from dual;--Output : 19.637 10 20

select floor(19.999) from dual;--Output : 19

select mod(100, 32) from dual;--Output : 4

select power(-5,3) from dual;--Output : -125

select sqrt(49) from dual;--Output : 7

select abs(-200) from dual;--Output : 200

select sign(-1) from dual;--Output : 0 -1

select SYSDATE, NEXT_DAY(SYSDATE, 'MONDAY'), LAST_DAY(SYSDATE) from dual;--Output : 28-FEB-14 03-MAR-14 28-FEB-14

select ROUND(SYSDATE, 'DAY'), TRUNC(SYSDATE, 'DAY'), 
       ROUND(SYSDATE, 'MONTH'), TRUNC(SYSDATE, 'MONTH'), 
       ROUND(SYSDATE, 'YEAR'), TRUNC(SYSDATE, 'YEAR') 
FROM DUAL;--Output : 02-MAR-14 23-FEB-14 01-MAR-14 01-FEB-14 01-JAN-14 01-JAN-14

-- TO_CHAR Function

select 1234, TO_CHAR(1234, '9999D99') from dual;--1234 1234.00  Convert into a character of a decimal format

select TO_CHAR(5634, '9.9EEEE') from dual;--  5.6E+03  Convert a number into a character of scientific notation

select TO_CHAR(1234567, '99G999G999') from dual;--  1,234,567  Grouping of numbers

select TO_CHAR(1234, 'L9999'), TO_CHAR(123, '9999L') from dual;--  $1234  123$  Setting locale to a number

select TO_CHAR(-1234, '9999MI'), TO_CHAR(1234, '9999MI') from dual;--1234- 1234  Trailing Minus if then umber is negative number

select TO_CHAR(-1234, 'L99G999D99MI') from DUAL;-- $1,234.00-  Trailing Minus with format if then umber is negative number

select TO_CHAR(-1234, 'L99G999D99PR') from DUAL;--  <$1,234.00> Negative Number Indicator

select 12, TO_CHAR(12, 'RN'), TO_CHAR(12, 'rn') from dual; -- 12 XII xii Returns Roman number RN for capital Roman and rn for small Roman Number

select 1000, TO_CHAR(1000, 'S9999'), TO_CHAR(-1000, 'S99999'), TO_CHAR(-1000, '99999S') from dual;-- Used for Signs to be placed ar front or rear

select 1000, TO_CHAR(1000.112, 'XXXX') from dual;-- 1000, 3E8 Converts number to a hexadecimal value. Can't accept -ve numbers. If no is not an integer, converts to a closest integer

select TO_CHAR(1234, '99,999.99') from dual;-- 1,234.00 Converts to a comma specified value

select TO_CHAR(1234, 'L99,999.99') from dual;-- $1,234.00 Converts to a locale specified decimal with comma separated values

select TO_CHAR(13453.23, '$99999.99') from dual;-- $13453.23 returns value with a leading dollar sign

select TO_CHAR(12345, '0999990') from dual;-- 012345 Zero indicator, Returns leading or trailing zeros

select 10000, TO_CHAR(1000, '0999999'), TO_CHAR(1000, '09999990') from dual;--10000 0001000 00001000

select 1000, 600, TO_CHAR(1000-600, '99999'), TO_CHAR(600-1000, '99999') from dual;-- Digit Place marker, provides space when positive and minus when negative

select 1000, TO_CHAR(1000, 'C9999999.99') from dual; -- 1000 USD1000.00 Specifies ISO Currency Symbol at the specified location

-- Date Format Functions:

select sysdate, TO_CHAR(SYSDATE, 'AD'), TO_CHAR(SYSDATE, 'B.C.') from dual; -- Indicates if specified date is AD or BC

select TO_CHAR(sysdate, 'A.M.') from dual;-- Indicates if it AM or PM based on the current date and time

select TO_CHAR(sysdate, 'CC-AD') from dual;-- Indicates which century it is

select TO_CHAR(sysdate, 'D') from dual;-- Indicates which number of day of a week it is

select TO_CHAR(SYSDATE, 'DAY') from dual;-- Indicates the spelled out day of a week in words

select TO_CHAR(SYSDATE, 'DD') from dual;-- Indicates which month day number of a particular month

select TO_CHAR(SYSDATE, 'DDD') from dual;-- Indicates which year day number of the particular year. This takes into consideration the leap year days too

select TO_CHAR(SYSDATE, 'DY') from dual;-- Indicates the abbreviated week day of a week

select TO_CHAR(SYSDATE, 'D-DY-DAY') from dual;--5-THU-THURSDAY 

select TO_CHAR(SYSDATE, 'IW') from dual;-- ISO Standard Week of the year indicator

select TO_CHAR(SYSDATE, 'IYYY'), TO_CHAR(SYSDATE, 'IYY'),TO_CHAR(SYSDATE, 'IY'),TO_CHAR(SYSDATE, 'I') from dual;-- ISO Standard Year format

select TO_CHAR(SYSDATE, 'YYYY'),TO_CHAR(SYSDATE, 'YYY'),TO_CHAR(SYSDATE, 'YY'),TO_CHAR(SYSDATE, 'Y') from dual;-- Similar to ISO Standard Year format

select TO_CHAR(SYSDATE, 'YEAR') from dual;-- Year spelled out

select TO_CHAR(SYSDATE, 'W') from dual;-- Returns week of the month

select TO_CHAR(SYSDATE, 'WW') from dual;-- Week of the year indicator

select TO_CHAR(SYSDATE, 'Q') from dual; -- Quarter of the year indicator

select TO_CHAR(SYSDATE, 'J') from dual;-- Julian Day of a given date

select TO_CHAR(SYSDATE, 'MM') from dual;-- Returns Numeric month of an year

select TO_CHAR(SYSDATE, 'MON') from dual;-- Returns Abbreviated month of an year

select TO_CHAR(SYSDATE, 'MONTH') from dual;-- Returns spelled out month of an year

select TO_CHAR(SYSDATE, 'HH') from dual;-- Returns Hours of the time from date time

select TO_CHAR(SYSDATE, 'MI') from dual;-- Returns minutes of the time from date time

select TO_CHAR(SYSDATE, 'HHAM'), TO_CHAR(SYSDATE, 'HH-AM') from dual;-- Returns Hours of the time from date time with AM or PM indicator

select TO_CHAR(SYSDATE, 'HH12'), TO_CHAR(SYSDATE, 'HH24') from dual;-- Returns Hour from time in 12 and 24 hour format

select TO_CHAR(SYSDATE, 'RM'), TO_CHAR(SYSDATE, 'DD-RM-YYYY') from dual; -- Returns Roman numeral month

select TO_CHAR(SYSDATE, 'SS') from dual;-- Returns Seconds from current date time

--These are the punctuation marks that can be used in date formats

select TO_CHAR(SYSDATE, 'DDth MON YYYY') from dual; -- th is used to suffix the number with st/nd/rd/th characters

select TO_CHAR(SYSDATE, 'DDsp MON YYYY') from dual;-- sp is used to spell out the number.

select TO_CHAR(SYSDATE, 'DD MON YYYYSPTH') from dual;-- We can use SP and TH in conjunction to a number to spell out the first and add a suffix from the listed keys

select TO_CHAR(SYSDATE, 'DDspth MON YYYYSP') from dual;-- Similar example as above  ## Output : TWENTY-SEVENTH FEB TWO THOUSAND FOURTEEN

select TO_CHAR(SYSDATE, 'FMDDspth MON YYYYSP') from dual;-- Fill Mode(FM) is used to suppress the blank padding in the return value of the TO_CHAR

--TO_NUMBER Function : Converts a character value to a number

select '$10,000.00', TO_NUMBER('$10,000.00', 'L99,999.99')+500 from dual;-- Converts a character number to a number format and added 500 to it

-- TO_DATE Function

select TO_CHAR(TO_DATE('12-FEB-2014'), 'DDSPTH') from dual;-- Converts a character dated value to a date and then spelled out the date

select first_name||' '||last_name, ADD_MONTHS(TO_DATE('1980-DECEMBER-17','YYYY-MONTH-DD'), 3) from employees;-- Adding 3 months to a date after converting from a character to a date

create table temp(a number, doj date);

insert into temp values(TO_NUMBER('1,23,456','9G99G999'), sysdate);

insert into temp values(TO_NUMBER('1,23,456-','9G99G999MI'), sysdate);

select * from temp;

insert into temp values(23412, TO_DATE('12-JAN-09 11:30:15 AM','DD-MON-YYYY HH:MI:SS AM'));

select TO_TIMESTAMP(doj) from temp;

alter table temp modify doj timestamp;

select TRUNC(TO_DATE('27-OCT-92', 'DD-MON-YYYY'), 'YEAR') from dual;--Truncates the date to the nearest year. Other parameters to pass are day and month

select ROUND(TO_DATE('27-OCT-92', 'DD-MON-YYYY'), 'YEAR') from dual;--01-JAN-93 Rounds the date to the nearest year. Other parameters to pass are day and month

select NEXT_DAY('27-FEB-14', 'WED') from dual;-- Gives the next wednesday. We can also pass the weekday spelled out like wednesday

select TO_CHAR(SYSDATE, 'FMMONTH, DD YYYY') from dual;

select TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR'), 'YYYY') from dual;--This converts the year and based on the parameter specified like RR or RRRR, returns the year from current year or previous year

select user, uid, userenv('SESSIONID'), userenv('LANGUAGE'), userenv('LANG'), userenv('INSTANCE') from dual;

select first_name, VSIZE(first_name), department_id, vsize(department_id) from employees where department_id = 100;

create table customers(ID NUMBER, NAME VARCHAR2(15), AGE NUMBER, ADDRESS VARCHAR2(20), Salary Number(10, 2));

create table ORDERS (OID NUMBER, ORDER_DATE DATE, CUSTOMER_ID NUMBER, AMOUNT Number(10, 2));

insert into CUSTOMERS VALUES(1, 'Ramesh', 32, 'Ahmedabad', 2000.00);
insert into CUSTOMERS VALUES(2, 'Khilan', 25, 'Delhi     ', 1500.00);
insert into CUSTOMERS VALUES(3, 'kaushik  ', 23, 'Kota', 2000.00);
insert into CUSTOMERS VALUES(4, 'Chaitali ', 25, 'Mumbai', 6500.00);
insert into CUSTOMERS VALUES(5, 'Hardik   ', 27, 'Bhopal', 8500.00);
insert into CUSTOMERS VALUES(6, 'Komal    ', 22, 'MP', 4500.00);
insert into CUSTOMERS VALUES(7, 'Muffy    ', 24, 'Indore', 10000.00);

insert into ORDERS VALUES(102, TO_DATE('2009-10-08', 'YYYY-MM-DD'), 3, 3000);
insert into ORDERS VALUES(100, TO_DATE('2009-10-08', 'YYYY-MM-DD'), 3, 1500);
insert into ORDERS VALUES(101, TO_DATE('2009-11-20', 'YYYY-MM-DD'), 2, 1560);
insert into ORDERS VALUES(103, TO_DATE('2008-5-20', 'YYYY-MM-DD'), 4, 2060);
insert into ORDERS VALUES(103, TO_DATE('2008-9-14', 'YYYY-MM-DD'), 8, 1230);

select * from ORDERS;

SELECT ID, NAME, AGE, AMOUNT
        FROM CUSTOMERS, ORDERS
        WHERE  CUSTOMERS.ID = ORDERS.CUSTOMER_ID(+);
        /*Output: 
        3 kaushik   23 3000
        3 kaushik   23 1500
        2 Khilan  25 1560
        4 Chaitali 25 2060
        5 Hardik   27 null
        1 Ramesh  32 null
        6 Komal     22 null
        7 Muffy     24 null
        */
        SELECT ID, NAME, AGE, AMOUNT
        FROM CUSTOMERS, ORDERS
        WHERE  CUSTOMERS.ID(+) = ORDERS.CUSTOMER_ID;
        /*Output : 
        2 Khilan 25 1560
        3 kaushik   23 1500
        3 kaushik   23 3000
        4 Chaitali 25 2060
        null      null       null    1230
        */

No comments:

Post a Comment