Date functions in Oracle

Hello
I need to rewrite the following sql I used in MSSQL server to sql for Oracle:
declare @lastmo_start datetime,
     @lastmo_end datetime
select @lastmo_start = dateadd(month, -1, getdate())
select @lastmo_end= dateadd(day, -1, getdate())
Basically I need to declare 2 variables and then set those values to the first day of the month and the last day of the same month.I will use these variables later in my script.
How I can do this in Oracle?
Thank you,
Lena

This syntax doesn't work.Do you mean it hurls an exception or just doesn't return the expected result? Please describe the expected and actual behaviour, with error messages if appropriate. Because it ought to work...
SQL> select * from dds;
        ID SDATE     EDATE
         1 11-FEB-06 11-MAR-06
         2 06-APR-06 11-APR-06
         3 11-APR-06 11-MAY-06
SQL> set serveroutput on
SQL> declare
  2    d1 date := trunc(sysdate, 'MM');
  3    d2 date := last_day(sysdate);
  4  begin
  5    for r in ( select * from dds
  6               where sdate >= d1 and edate <= d2 )
  7    loop
  8       dbms_output.put_line(r.id||'::'||r.sdate||'::'||r.edate);
  9    end loop;
10  end;
11  /
2::06-APR-06::11-APR-06
PL/SQL procedure successfully completed.
SQL> Cheers, APC

Similar Messages

  • Raw to date function in oracle

    Hi..
    I would like to know the details and usage of Raw to date function in oracle database.Could some one let me know the function details?

    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> set serveroutput on
    SQL> DECLARE
      2   rv RAW(32) := '786B060818023A';
      3   dt DATE := NULL;
      4  BEGIN
      5    dbms_stats.convert_raw_value(rv, dt);
      6    dbms_output.put_line(TO_CHAR(dt, 'DD-MON-YYYY'));
      7  END;
      8  /
    08-JUN-2007
    PL/SQL procedure successfully completed.
    SQL>Regards,
    Jo
    PS: This is not my own example. I don't recollect the link I got this example from. Will post the link if I come across it again. I think its www.psoug.org (not sure)

  • Convert Date function to oracle

    The follwoing Sql for Sqlserver, what is the equivalent function in oracle. Need help on this.
    YTD:
    YOUR_DATE_FIELD between DATEADD(yy,
    DATEDIFF(yy,0,@Prompt('BEGIN_DATE','D',,mono,free)), 0) AND
    @Prompt('END_DATE','D',,mono,free)
    MTD:
    YOUR_DATE_FIELD between DATEADD(mm, DATEDIFF(mm, 0,
    ,@Prompt('BEGIN_DATE','D',,mono,free)), 0) AND
    @Prompt('END_DATE','D',,mono,free)
    WTD
    YOUR_DATE_FIELD between (,@Prompt('BEGIN_DATE','D',,mono,free) -
    DATEPART(dw, ,@Prompt('BEGIN_DATE','D',,mono,free)))+1 AND
    @Prompt('END_DATE','D',,mono,free)

    Extracting month,year,day from sysdate (You may even opt to use to_char)
    SQL> select extract (month from sysdate) from dual;
    EXTRACT(MONTHFROMSYSDATE)
                            4
    SQL> select extract (year from sysdate) from dual;
    EXTRACT(YEARFROMSYSDATE)
                        2007
    SQL> select extract (day from sysdate) from dual;
    EXTRACT(DAYFROMSYSDATE)
                          5
    Adding 5 days to today's date
    SQL> select sysdate + 5 from dual;
    SYSDATE+5
    10-APR-07
    Adding Months
    SQL> select add_months(sysdate,3) from dual;
    ADD_MONTH
    05-JUL-07
    Date Difference
    SQL> select sysdate - to_date('01/04/2007','dd/mm/yyyy') from dual;
    SYSDATE-TO_DATE('01/04/2007','DD/MM/YYYY')
                                    4.42670139
    Date Difference
    SQL> select trunc(sysdate) - trunc(to_date('01/04/2007','dd/mm/yyyy')) from dual
    TRUNC(SYSDATE)-TRUNC(TO_DATE('01/04/2007','DD/MM/YYYY'))
                                                           4
    SQL>

  • Date functions in Oracle 8i Lite

    When i am trying to type in the following command in my win98 o/s:
    select to_char(to_date('01-jul-1997','dd-mon-yyyy'),'day ww')from dual
    My sql*plus returns the following error message:
    OCA-30021: error preparing/executing SQL statement
    [POL-2423] invalid value for date/time field
    However, I can run this statement under the 8i lite WINNT environment. I want to know whether this is due to limitation of 8i lite running on Win98 or there is some problem with my configuration of Oracle 8i?
    null

    This syntax doesn't work.Do you mean it hurls an exception or just doesn't return the expected result? Please describe the expected and actual behaviour, with error messages if appropriate. Because it ought to work...
    SQL> select * from dds;
            ID SDATE     EDATE
             1 11-FEB-06 11-MAR-06
             2 06-APR-06 11-APR-06
             3 11-APR-06 11-MAY-06
    SQL> set serveroutput on
    SQL> declare
      2    d1 date := trunc(sysdate, 'MM');
      3    d2 date := last_day(sysdate);
      4  begin
      5    for r in ( select * from dds
      6               where sdate >= d1 and edate <= d2 )
      7    loop
      8       dbms_output.put_line(r.id||'::'||r.sdate||'::'||r.edate);
      9    end loop;
    10  end;
    11  /
    2::06-APR-06::11-APR-06
    PL/SQL procedure successfully completed.
    SQL> Cheers, APC

  • Imbed Date Function????

    Hi,
    I am having trouble with a date function in Oracle Administrator. How do I make max_date a condition (for a case statement)? I am trying to return a value (a ranking) for the most recent ranking date.
    Ie:
    Case
    When Date = Max --I know you can use a Max(date) function but 
    and (another condition) --how do you make that a condition within case
    Then (return value) -- statement?
    Else 0
    End
    Is this a case where I have to import an Oracle Function? Or can I get around this by using a pre-existing function?

    Multiple posting
    Date Function - Holiday?

  • Using R function on Oracle data base

    for using R, I only can use Oracle data mining algorithms or I can use R function like social network in Oracle data base?

    Oracle Advanced Analytics provides multiple language (SQL, R and if we include GUIs, Oracle Data Miner workflow UI) support to the in-DB analytics. Using SQL, R or the ODM'r GUI, you can access a library of simple descriptive, summary and coparative statistics that have been implemented in the Oracle Database + a dozen hi-perf in-DB data mining algorithms (e.g. regression, decision trees, text mining, clustering, associations, anomaly detection, etc.) and solve a wide range of business and technical problems.
    Should you require additional techniques e.g. R CRAN packages, then Oracle Advanced Analytics supports that through R to SQL transparancy where it "pushes down" R language intentions to matching in-DB SQL functions e.g. ETL, stats and data mining OR it can make a callout to R via "embedded R mode" and have the Database manage the flow of data and the process.
    There is an OTN Discussion Forum that focuses entirely on the "R" aspects of the Oracle Advanced Analytics Option. I suggest that you re-post your questiion here R Technologies for additional discusssions and for the benefit of that Oracle Advanced Analytics/Oracle R Enterprise OTN forum/community.
    cb

  • Data Federator Universe Date Functions

    Hi,
    I created a Data Federator Universe from target tables (Source Tables from: Sql Server 2005 and Oracle 10g). Now, I want to create a object in the universe: "Days between 2 dates"(coming from 2 different target tables). I don't see any other date functions other than CURDATE(). How to create my object?
    Alternatively, Can I create a caliculated column in the existing target table? For Example, I want to create a new column "Days between 2 Dates" from 2 different tables by using a formula in Default mapping of the target table.
    Thanks & Regards,
    Peter

    Hi Amit,
    Thanks for your reply.
    Ok. So, Universe on top of Data Federator has limited functionality.
    And, other option you mentioned is on report level. I am creating an adhoc universe and I have few objects which will calculate days between 2 dates coming from 2 different tables.
    But, how can I achieve this on Data Federator level. I have no function there to find Days Between 2 dates. I see lot of time and date functions but not the one I required. Also, I added a column in the target table and tried to apply the formula there in the default mapping area. But, I see only the selected target table. I need another date column from another table, which is not displayed in the default mapping area.
    How can I achieve this?
    Regards,
    -Peter

  • How to update date field in oracle using jdbc

    Hi all,
    In my database i have a column LAST_UPDATED_DTTM which is used to store last updated date . when i try to update using the following query stmt
    update MD_TABLE set LAST_UPDATED_DTTM= + " new java.sql.Date(c.getTime().getTime()) where PROD_ID=" + msdn ;
    ' c ' is instance of Calendar Class as below
    Calendar c=Calendar.getInstance()
    it gives the following error
    ORA009:Expected date got number
    can anybody please help with this?
    Thank you

    can't i do that without prepare statement?If you insist on stuffing that String, then format it in some convenient String format using SimpleDateFormat and call the TO_DATE function in Oracle, so that the database understands your Date too.
    ***Annie***

  • Converting numeric date (seconds) to Oracle date

    Hello all,
    Not sure if this is the correct place for my question but here goes...
    I am extracting data from an Oracle table in which the date/time is stored as a number (seconds since 1970). How can I convert this to an Oracle recognized date? I tried to extract the date as a number and then convert it using the localtime function in PERL but ran into other problems. Any help or advice would be greatly appreciated.
    David

    Instead of writing PERL script if you write on a format trigger same coad (using Pl/SQL) it will work.

  • Need Assistance for VBA function in oracle how to implement

    My very respected and Senior, regards,
    Sir, hope you will in best of health and wealth by the grace of God,
    Sir, i have a request problem as i m very junior against you and you have passed this time before many years ago as i m standing where. Sir i m a very junior developer of oracle and have a problem putting on your desk with the hope that you can help my as a boss.
    Sir me have to calculate yield of Bond using oracle form
    i have tried my best and tired
    there is a formulae in excel which give the Yield() when we provide the parameters
    and i need the excel formulae or the oracle calculation code of PLSQL for this.
    How can i get yield when i have price, coupon rate, frequency, issue and maturity of the coupon , coupon period , next coming coupon , and others detail.
    or tell me how to use EXCEL VBA for this problem ,
    thnx n regards,
    yours student, junior developer youngest brother
    Faraz
    How can I get the solution using Excel VBA function with oracle
    so that move values to excel calculate them and copy the result from excel to oracle forms

    Hi,
    for the Hex-Number-conversion see:
    [url http://psoug.org/snippet/Convert-Hex-to-Decimal-Decimal-to-Hex_78.htm] self-defined Conversion-Functions
    What number format do you have? YYYMMDD
    Or is there a Date corresponding to 1 and a number n represent the date n-1 days after day 1?
    Please describe further.
    Bye
    stratmo

  • Equivalent of DB2  functions in ORACLE 11g

    Hi,
    I am trying to convert the SQL queries written in DB2 to ORACLE. There are some db2 specific functions are used in the queries.I am not able to find the equivalent function in ORACLE. The function names are written below:
    1) DateDD()
    2) SELECT @@IDENTITY
    3) SELECT *
    FROM (
    SELECT ROWNUMBER() OVER() AS rowId_1, INNER_TABLE.*
    FROM (----)as innertable
    ) AS Outertable
    Error is: ROWNUMBER is INVALID identifier.
    4) DAYOFWEEK()
    5) DAYS()
    6) dayofyear()
    Please help me in finding the equivalent function in ORACLE.
    Thanks in advance!!

    You probably don't need a DateAdd function in Oracle. You can add a number to a date in Oracle-- that adds the number of days to the date.
    SELECT sysdate today, sysdate+1 tomorrow
      FROM dualWhy are you using DAYS()? If, as in the example, you're just trying to determine the number of days between two dates, you can subtract dates in Oracle and the difference will be a number of days (including a fractional component if applicable)
    SELECT date '2011-09-27' - date '2011-09-25' difference_in_days
      FROM dualIf you really need the number of days since January 1, 0001, you could subtract the date from Jan 1, 0001, i.e.
    SELECT date '2011-09-27' - date '0001-01-01'
      FROM dualI would assume that Oracle and DB2 would return the same number but there could well be some differences since the current calendar didn't exist in the year 1 and I know there were issues in the transition from the Gregorian to the Julian calendar where some days were decreed not to exist. It wouldn't shock me if Oracle and DB2 counted some of the days in the 1500's differently.
    Justin

  • Import Functionality in Oracle ADF BC with Jdeveloper 11.1.1.2

    Hi all,
    I am using Jdeveloper 11.1.1.2 and I am trying to import functionality in my application.
    I am following the steps of: http://andrejusb.blogspot.com/2008/07/import-functionality-in-oracle-adf-bc.html
    I have 2 data model and I would like to import the functionality of one data model, in the other.
    My problem is when I tried: myModel2-->right click-->project properties-->deployment-->edit
    I don't see nothing to filter myModel2, I see only Library Dependencies, Connections, JAR Options ADF Validation.
    If I tried : myModel2-->right click-->project properties-->deployment-->new-->filters
    I see only the folders: Merged Contents of this file Group's contributor, .data, 00000000
    But I can't see the folders I have (mypackage.test.model.entities).
    Where I can found them? Is there a workaround?
    Any suggestions?
    Thanks
    Andrea

    Ok, but I taught that after importing myModel2 in myModel1, Jdeveloper realise a merge and it visualizes a unique package, even if it is splitted on two or more projects/models.
    So that, it seems to me this procedure is useful for importing jar file, but not for splitting a very big project in a lot of smaller projects, doesn't it?
    Thanks for your opinion
    Andrea

  • ....Date Type in Oracle

    Hi All,
    Just to clear a few misconception/ambiguities regarding the DATE data type in Oracle.
    We hve a table which stores the date of insertion of a record. The front end application is in PHP while backend is in oracle.
    When i query the table, it shows the date as dd-mon-yy , but it does not show the time. I have tried to query it throgh sqlPLus , TOAD and the PHP Application, but it doesnt show the time.
    Is it not storing the time. The data type of the column is ( Insertdate date).
    If you query sysdate, it definitly stores time, ... but what is in this case. Is it just storing the date and not the time. ???
    How to alter so that it stores time as well.
    Regards

    U have to use the conversion function to_date to
    store date along with time. No, you use to_date to convert a string to a date. That's it. A date column will ALWAYS store the time (it just won't always display it)
    When u need to retrieve the data use to_char function. Not necessarily:
    SQL> create table x (dte date);
    Table created.
    SQL> insert into x values(sysdate);
    1 row created.
    SQL> insert into x values(sysdate+1);
    1 row created.
    SQL> select * from x;
    DTE
    11-jul-2007 09:13:11
    12-jul-2007 09:13:11
    if u want to store time u can use timestamp data typeNope, You use timestamp if you want to store fractions of seconds (down to nano seconds)
    The key point is. The date datatype ALWAYS stores:
    Century, Year, Month, Day, Hour, Minute, Second

  • Date question in Oracle

    hi
    how to insert (text) time: 09:44:02 and date:11/09/2007 to date filed in Oracle ?
    i have field Tdate (date) and Ttime (date) in oracle
    thanks in advance
    Edited by: GoldSoft on 16:33 22/08/2011

    Gold, I think SB has answered your question. You use to_date to pass the character representation of a date to Oracle and identify what format the date is in using a format mask. To get a date data type out of Oracle and present it any way you want you use the to_char function and a format mask to tell Oracle how to display the date information.
    You can find the date format mask characters listed in the SQL Language manual. Just look up to_date or to_char and there should be a reference to the available masks.
    HTH -- Mark D Powell --

  • Recursive functions in Oracle SQL.

    OK,
    Here is the pivot problem. Only this time were on an Oracle 11.1.0.7.0.
    I've created a table called:
    CREATE TABLE STAGING.MY_TAB_COLS
      TABLE_NAME   VARCHAR2(30 BYTE),
      COLUMN_NAME  VARCHAR2(30 BYTE)
    )And I've put the following data into it:
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'SSN_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'FIRST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'LAST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'DOB_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_SSN_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_FIRST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_LAST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_DOB_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'MPI');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'SSN_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'FIRST_NAME_A_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'FIRST_NAME_B_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'LAST_NAME_A_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'LAST_NAME_B_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_SSN_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_FIRST_NAME_A_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_FIRST_NAME_B_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_LAST_NAME_A_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAEADP1', 'EMP_LAST_NAME_B_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'MPI_OLD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_SSN_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_LAST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_FIRST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_DOB');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_SSN');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_LAST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_FIRST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_DOB');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_NON_SSN_ID');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_SSN_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_FIRST_NAME_A_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_FIRST_NAME_B_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_LAST_NAME_A_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_LAST_NAME_B_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_FIRST_NAME_A_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_FIRST_NAME_B_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_LAST_NAME_A_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'EMPLOYEE_LAST_NAME_B_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'PATIENT_SSN_STANDARDIZED');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAMAET1', 'MPI');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'EMPLOYEE_SSN');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'EMPLOYEE_FIRST_NAME');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'EMPLOYEE_LAST_NAME');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'EMPLOYEE_DATE_OF_BIRTH');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_SSN');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_FIRST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_LAST_NAME_SOURCE');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_DOB');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PRESCRIBER_LAST_NAME');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'MPI');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_FIRST_NAME_A_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_FIRST_NAME_B_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_LAST_NAME_A_STD');
    Insert into STAGING.MY_TAB_COLS
       (TABLE_NAME, COLUMN_NAME)
    Values
       ('AAPMED1', 'PATIENT_LAST_NAME_B_STD');My task for this problem is pretty straight forward.
    Using the ALL_TAB_COLS table I want to list out certain fields for each table name specified.
    All the tables have fields that are similar. I just want these table names to go across the top.
    And the field names to list out under each table.
    So you would have something like:
    AAEADP1                         AAMAET1                     AAPMED1
    SSN_SOURCE                      MPI_OLD                     EMPLOYEE_SSN
    FIRST_NAME_SOURCE               EMPLOYEE_SSN_SOURCE         EMPLOYEE_FIRST_NAME
    LAST_NAME_SOURCE                EMPLOYEE_LAST_NAME_SOURCE   EMPLOYEE_LAST_NAME
    DOB_SOURCE                      EMPLOYEE_FIRST_NAME_SOURCE  EMPLOYEE_DATE_OF_BIRTH
    EMP_SSN_SOURCE                  EMPLOYEE_DOB                PATIENT_SSN
    EMP_FIRST_NAME_SOURCE           PATIENT_SSN                 PATIENT_FIRST_NAME_SOURCE
    EMP_LAST_NAME_SOURCE            PATIENT_LAST_NAME_SOURCE    PATIENT_LAST_NAME_SOURCE
    EMP_DOB_SOURCE                  PATIENT_FIRST_NAME_SOURCE   PATIENT_DOB
    MPI                             PATIENT_DOB                 PRESCRIBER_LAST_NAME
    SSN_STANDARDIZED                EMPLOYEE_NON_SSN_ID         MPI
    FIRST_NAME_A_STANDARDIZED       EMPLOYEE_SSN_STANDARDIZED   PATIENT_FIRST_NAME_A_STD
    FIRST_NAME_B_STANDARDIZED       PATIENT_FIRST_NAME_A_STD    PATIENT_FIRST_NAME_B_STD
    LAST_NAME_A_STANDARDIZED        PATIENT_FIRST_NAME_B_STD    PATIENT_LAST_NAME_A_STD
    LAST_NAME_B_STANDARDIZED        PATIENT_LAST_NAME_A_STD     PATIENT_LAST_NAME_B_STD
    EMP_SSN_STANDARDIZED            PATIENT_LAST_NAME_B_STD   
    EMP_FIRST_NAME_A_STANDARDIZED   EMPLOYEE_FIRST_NAME_A_STD   
    EMP_FIRST_NAME_B_STANDARDIZED   EMPLOYEE_FIRST_NAME_B_STD   
    EMP_LAST_NAME_A_STANDARDIZED    EMPLOYEE_LAST_NAME_A_STD   
    EMP_LAST_NAME_B_STANDARDIZED    EMPLOYEE_LAST_NAME_B_STD   
                                    PATIENT_SSN_STANDARDIZED   
                                    MPI         Where each table name lists across with all of their column names under each table.
    The query that I have so far is The following:
      SELECT   T1.TABLE_NAME, T1.COLUMN_NAME
        FROM   my_tab_cols t1, (  SELECT   DISTINCT UPPER (SOURCE_TABLE) TABLE_NAME
                                     FROM MPI_DEMOGRAPHICS_TEST
                                 ORDER BY UPPER (SOURCE_TABLE)) tn
       WHERE   T1.TABLE_NAME = tn.TABLE_NAME
               AND (   T1.column_name LIKE '%MPI%'
                    OR T1.column_name LIKE '%SSN%'
                    OR T1.column_name LIKE '%E%SSN%'
                    OR T1.column_name LIKE '%R%SSN%'
                    OR T1.column_name LIKE '%P%SSN%'
                    OR T1.column_name LIKE '%BIRTH%'
                    OR T1.column_name LIKE '%DOB%'
                    OR T1.column_name LIKE '%FIRST%NAME%'
                    OR T1.column_name LIKE '%LAST%NAME%'
                    OR T1.column_name LIKE '%CLIENT%NAME%'
                    OR T1.column_name LIKE '%SOURCE_TABLE%'
                    OR T1.column_name LIKE '%TABLE%')
    ORDER BY   T1.TABLE_NAME, t1.column_id; It just list everything straight down.
    Where the MPI_DEMOGRAPHIES_TEST table feeds the relevant table names to the ALL_TAB_COLS table.
    It can just be substituted with a list of the values, AAEADP1, AAMAET1, AAPMED1.
    Is there a 'pivot' function in Oracle 11.1 that I can use to list these column names out sideways as opposed to what I did before?
    Thanks,

    Hi,
    Here's one way to get results like that using the Oracle 11 SELECT ... PIVOT feature:
    WITH     got_nums     AS
         SELECT     column_name
         ,     ROW_NUMBER () OVER ( PARTITION BY  table_name
                                   ORDER BY          column_name
                           )                    AS r_num
         ,     DENSE_RANK () OVER ( ORDER BY      table_name)     AS c_num
         FROM     my_tab_cols
            WHERE   column_name     LIKE '%MPI%'
            OR     column_name     LIKE '%SSN%'
    --      OR     column_name     LIKE '%E%SSN%'          -- Included in '%SSN% above
    --      OR     column_name     LIKE '%R%SSN%'          -- Included in '%SSN% above
    --      OR     column_name     LIKE '%P%SSN%'          -- Included in '%SSN% above
            OR     column_name     LIKE '%BIRTH%'
            OR     column_name     LIKE '%DOB%'
            OR     column_name     LIKE '%FIRST%NAME%'
            OR     column_name     LIKE '%LAST%NAME%'
            OR     column_name     LIKE '%CLIENT%NAME%'
    --      OR     column_name     LIKE '%SOURCE_TABLE%'     -- Included in %TABLE% below
            OR     column_name     LIKE '%TABLE%'
        UNION
            SELECT  table_name                         AS colum_name
         ,     0                              AS r_num
         ,     DENSE_RANK () OVER ( ORDER BY      table_name)     AS c_num
         FROM     my_tab_cols
    SELECT       *
    FROM       got_nums
    PIVOT       (   MIN (column_name)
           FOR c_num     IN ( 1     AS table_1
                            , 2     AS table_2
                      , 3     AS table_3
    ORDER BY  r_num
    ;Output:
    R_NUM TABLE_1                        TABLE_2                        TABLE_3
        0 AAEADP1                        AAMAET1                        AAPMED1
        1 DOB_SOURCE                     EMPLOYEE_DOB                   EMPLOYEE_DATE_OF_BIRTH
        2 EMP_DOB_SOURCE                 EMPLOYEE_FIRST_NAME_A_STD      EMPLOYEE_FIRST_NAME
        3 EMP_FIRST_NAME_A_STANDARDIZED  EMPLOYEE_FIRST_NAME_B_STD      EMPLOYEE_LAST_NAME
        4 EMP_FIRST_NAME_B_STANDARDIZED  EMPLOYEE_FIRST_NAME_SOURCE     EMPLOYEE_SSN
        5 EMP_FIRST_NAME_SOURCE          EMPLOYEE_LAST_NAME_A_STD       MPI
        6 EMP_LAST_NAME_A_STANDARDIZED   EMPLOYEE_LAST_NAME_B_STD       PATIENT_DOB
        7 EMP_LAST_NAME_B_STANDARDIZED   EMPLOYEE_LAST_NAME_SOURCE      PATIENT_FIRST_NAME_A_STD
        8 EMP_LAST_NAME_SOURCE           EMPLOYEE_NON_SSN_ID            PATIENT_FIRST_NAME_B_STD
        9 EMP_SSN_SOURCE                 EMPLOYEE_SSN_SOURCE            PATIENT_FIRST_NAME_SOURCE
       10 EMP_SSN_STANDARDIZED           EMPLOYEE_SSN_STANDARDIZED      PATIENT_LAST_NAME_A_STD
       11 FIRST_NAME_A_STANDARDIZED      MPI                            PATIENT_LAST_NAME_B_STD
       12 FIRST_NAME_B_STANDARDIZED      MPI_OLD                        PATIENT_LAST_NAME_SOURCE
       13 LAST_NAME_A_STANDARDIZED       PATIENT_DOB                    PATIENT_SSN
       14 LAST_NAME_B_STANDARDIZED       PATIENT_FIRST_NAME_A_STD       PRESCRIBER_LAST_NAME
       15 LAST_NAME_SOURCE               PATIENT_FIRST_NAME_B_STD
       16 MPI                            PATIENT_FIRST_NAME_SOURCE
       17 SSN_SOURCE                     PATIENT_LAST_NAME_A_STD
       18 SSN_STANDARDIZED               PATIENT_LAST_NAME_B_STD
       19                                PATIENT_LAST_NAME_SOURCE
       20                                PATIENT_SSN
       21                                PATIENT_SSN_STANDARDIZEDIf you don't want to see the r_num column, use your front-end to hide it (e.g., in SQL*Plus: "COLUMN r_num NOPRINT"), or do the pivot in a sub-query, and only select table_1, table_2 and table_3 in the main query.
    As with all pivots, you have to hard-code an upper bound to the number of pivoted columns. I used 3 above. You could use 4, or 5, or 45, but you must specify exactly how many columns to display.
    If you say there will be 3 tables, and there actually are more than 3, then the query will still run, but all the tables after the first 3 will be ignored.
    If you say there will be 3 tables, and there actually are fewer, then the query will still run, but you will have NULL columns at the end of each output row.
    This sorts the output alphabetically by table_name and column_name. You can sort by anything you want by changing the analytic ORDER BY clauses. For example, if you are using all_tabl_columns and you want the columns to appear in order by column_id, then you would say:
    ,     ROW_NUMBER () OVER ( PARTITION BY  table_name
                              ORDER BY          column_id     -- instead of column_name
                      )                    AS r_num

Maybe you are looking for