Using TRIM function in select statement

Hi All,
I'm using the TRIM function in my select statement to eliminate the white spaces.
But while using in select the TRIM function is not working in SQL*PLUS client(The query returns the white spaces also)
Kindly provide some pointers regarding the issue.........
I want to get only the data without the spaces in select statement
Regards,
Mohan

Hi, Mohan,
SQL*Plus always pads columns to make them line up nicely.
If you have a column declared as VARCHAR2 (20), then SQL*Plus will normally display 20 characters for that column, even in the maximum actual length is, say, 5 (or even if the column always happens to be NULL).
If you want the output to include only the actual data, without the padding that SQL*Plus adds, then concatenate all the columns into one big string column.
People often do something like the following to generate a CSV file, with no exta spaces:
SELECT       TO_CHAR (empno)
|| ',' || ename
|| ',' || job
|| ',' || TO_CHAR (deptno)
|| ',' || TO_CHAR (hiredate, 'DD-Mon-YYYY')     AS all_data
FROM          scott.emp;

Similar Messages

  • Using java function in select statement

    Hi,
    I am trying to use java function in select statement.
    public class ClassA{
         private static String MyConst = "foo";
         public static String functionA(){
              return MyConst;
    in my query I have:
    select
         ClassA.functionA() AS id,
         groupId AS newID,
    from
         myChannel[now]
    ClassA is part of the application (no need to import).
    I get and error of Invalid Expression on ClassA.functionA().
    I also tried to declare the function in the processor element:
    <wlevs:processor id="proc">
         <wlevs:function function-name="A" exec-methode="functionA">
              <bean class="mtPackage.ClassA"/>
         </wlevs:function>
    <wlevs:processor>
    but then I get a different error in the processor XML file:  "An InvocationTargetException was encoutered while attemting to register the user defind function A. The message was null"
    What am I missing here?

    Hi,
    From the above description, you have tried two manners to call method functionA() in the user defined  class ClassA. One uses java cartridge manner directly and the other try to use user defined function manner.
    For the java cartridge manner, the following CQL query should work if the ClassA is really included in the OEP app. I have done similar test before, it works.
    select
         ClassA.functionA() AS id,
         groupId AS newID,
    from
         myChannel[now]
    For user defined function manner, I think two things you need to change:
    1. Need to declare the function in the EPN assembly file(under META-INF/spring/), not component configuration file(under META-INF/wlevs/). The following is an example:
    <wlevs:processor id="proc">
         <wlevs:function function-name="A" exec-methode="functionA">
              <bean class="mtPackage.ClassA"/>
         </wlevs:function>
    </wlevs:processor>
    2. Call the user defined function in the CQL query in the component configuration file under processor. For example:
    select A() from myChannel
    Regards,
    XiYing

  • Using function in select statement

    Hi,
    CREATE FUNCTION [dbo].[udf_testFunction] 
    @value int
    RETURNS int
    AS
    BEGIN
    -- Declare the return variable here
    declare @returnValue int
    set @returnValue = @value*2;
    return @returnValue;
    END
    GO
    create table #Temp
        EmpID int, 
        EmpName Varchar(50)
    insert into #Temp(EmpID,EmpName) values(1,'Name1');
    insert into #Temp(EmpID,EmpName) values(2,'Name2');
    insert into #Temp(EmpID,EmpName) values(3,'Name3');
    insert into #Temp(EmpID,EmpName) values(4,'Name4');
    insert into #Temp(EmpID,EmpName) values(5,'Name5');
    select EmpID,EmpName, [dbo].[udf_testFunction](EmpID), [dbo].[udf_testFunction](EmpID)*EmpID,[dbo].[udf_testFunction](EmpID)+2 from #Temp
    In the above select statement i used [dbo].[udf_testFunction]() function 3 times. But i want to use only once and reuse it.
    Something like 
    select EmpID,EmpName, testfunctionvalue,testfunctionvalue*EmpID,testfunctionvalue+2 from #Temp
    Please advise me.... Thanks in Advance...

    You can use this code: 
    WITH cte
    AS ( SELECT EmpID ,
    EmpName ,
    [dbo].[udf_testFunction](EmpID) AS testfunctionvalue
    FROM #Temp
    SELECT EmpID ,
    EmpName ,
    testfunctionvalue ,
    testfunctionvalue * EmpID ,
    testfunctionvalue + 2
    FROM cte
    But using scalar functions in select clause can hurt the performance. Please see this link: 
    SQL Server Scalar User Defined Function Performance
    T-SQL Articles
    T-SQL e-book by TechNet Wiki Community
    T-SQL blog

  • PL/SQL Function in Select statement

    Hi
    I am calling a function in select statement. The query works in Toad and PL/SQL developer except SQL plus and disconnecting Oracle connection.
    The error is “ERROR at line 1: ORA-03113: end-of-file on communication channel”.
    When I called the same query from BC4J View Object the error message is “java.sql.SQLException: No more data to read from socket”.
    Can any one advise me please?
    Thanks
    Srini

    Srini
    I've seen similar cases in the past with 9.2.0.x (x <= 5). What Oracle version are you using? It's worth checking the bug database (I can't just now - I don't have a valid support id in my current contract). And as Warren says, post your SQL query.
    HTH
    Regards nigel

  • How to use a function in select clause

    hi gems...good evening...
    I want to write a select clause from a function. The scenario is like below:
    function parameters
    CREATE OR REPLACE FUNCTION FUNCTION_DEMO(p_pid IN NUMBER,
    p_bankerNum IN NUMBER,
    p_banker_name IN VARCHAR2,
    p_business_num IN NUMBER,
    p_businesses IN table_typ_businesses)
    return table_typ_bankers
    as .......
    global object and table types
    CREATE OR REPLACE TYPE obj_typ_businesses AS OBJECT (businesses NUMBER);
    CREATE OR REPLACE TYPE table_typ_businesses AS TABLE OF obj_typ_businesses;
    CREATE OR REPLACE TYPE obj_typ_bankers AS OBJECT (banker_num NUMBER(10,0));
    CREATE OR REPLACE TYPE table_typ_bankers IS TABLE OF obj_typ_bankers;
    The select query
    select * from
    table(cast(FUNCTION_DEMO(1,
    2,
    'ALEX',
    2,
    table(cast(select businesses_id from reference_businesses) as table_typ_businesses) as table_typ_bankers)But it is giving error with Missing expression.
    My function is compiled successfully. I just want to make the select query to view the output given by the function i.e the table type "table_typ_bankers".
    Please help...thanks in advance..

    Hi ,
    You can check this and change your code by taking this as an example. I just noted down with simple one input and return as collection type
    and how to use/call it in select statement.
    SQL> CREATE OR REPLACE TYPE obj_typ_businesses AS OBJECT (businesses NUMBER)
      2  /
    Type created.
    SQL> CREATE OR REPLACE TYPE table_typ_businesses AS TABLE OF obj_typ_businesses
      2  /
    Type created.
    SQL> CREATE OR REPLACE TYPE obj_typ_bankers AS OBJECT (banker_num NUMBER(10,2))
      2  /
    Type created.
    SQL> CREATE OR REPLACE TYPE table_typ_bankers IS TABLE OF obj_typ_bankers
      2  /
    Type created.
    /* Now creating a demo function and just focusing on your one input as collection type  */
    SQL> create or replace function f11 ( tab_rec  IN table_typ_businesses)
      2  return table_typ_bankers
      3  is
      4  t_val table_typ_bankers:= table_typ_bankers();
      5  begin
      6        dbms_output.put_line('tab_rec count -'||tab_rec.count);
      7
      8  for i in 1..tab_rec.count
      9              loop
    10               dbms_output.put_line ('tab_rec value ('||i||')'||tab_rec(i).businesses);
    11                       t_val.EXTEND;
    12                    t_val(i) := obj_typ_bankers(tab_rec(i).businesses * 12.50);
    13              end loop;
    14              return t_val;
    15  end;
    16  /
    Function created.
    /* To run your function by a select statement */
    SQL> SELECT * FROM TABLE(f11(table_typ_businesses(obj_typ_businesses(5),
      2                          obj_typ_businesses(12),
      3                                         obj_typ_businesses(18))
      4                                             ) )
      5  /
    BANKER_NUM
          62.5
           150
           225
    tab_rec count -3
    tab_rec value (1)5
    tab_rec value (2)12
    tab_rec value (3)18Hope this will help you... :)
    Thanks!
    Ashutosh

  • Error when trying to use trim function

    when i try to use trim function on a column it gives me following error for the below query
    select trim(first_name) name from table1@pa1;
    ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
    DSNT408I SQLCODE = -440, ERROR: NO FUNCTION BY THE NAME TRIM HAVING COMPATIBLE ARGUMENTS WAS FOUND IN THE CURRENT PATH DSNT418I SQLSTATE = 42884 SQLSTATE RETURN CODE DSNT415I SQLERRP = DSNXORFN SQL PROCEDURE DETECTING ERROR DSNT416I SQLERRD = -100 0 0 -1 0 0 SQL DIAGNOSTIC INFORMATION DSNT416I SQLERRD = X'FFFFFF
    any idea gurus ???Thanks

    This might work:
    select trim(first_name) name from
      (select first_name from table1@pa1);The problem is that pa1 is a non-Oracle database and does not have a trim function. It may have another function that does the same thing but it's not called TRIM.

  • Error by using database procedure in select statement

    hi ,
    I have built a database procedure having one parameter with in out varchar type. that return value with some addition.
    i am using it with some column in select statement only for display purpuses but i am facing error by doing that in select statement. that procedure is working well with bind variable but not with select statement.
    plz help me how i can use a procedure in select statement. or can i do it or not.

    plz help me how i can use a procedure in select statement. or can i do it or not.A workaround could be to create a wrapper function for your procedure. One that only passes the input parameters and returns the output parameter.
    The simply call this function in your select which internally calls the procedure.

  • Problem in using aggregate functions inside case statement

    Hi All,
    I am facing problem while using aggregate functions inside case statement.
    CASE WHEN PSTYPE='S' THEN MAX(DECODE(POS.PBS,1,ABS(POS.PPRTQ),0)) ELSE SUM(DECODE(POS.PBS,1,ABS(POS.PPRTQ),0)) END,
    how can I achieve above requirement ? Con anyone help me.
    Thanks and Regards
    DG

    Hi All,
    Below is my query:
            SELECT
            CASE WHEN p_reportid IN ('POS_RV_SN','POS_PB') THEN POS.PACCT
            ELSE POS.PACCT || '-' || DECODE(POS.SYSTEMCODE,'GMI1','1', 'GMI2','2', 'GMI3','4', 'GMI4','3', '0') ||POS.PFIRM|| NVL(POS.POFFIC,'000') END,
            CASE WHEN p_reportid IN ('POS_RV_SN','POS_PB') THEN POS.PACCT||POS.PCUSIP||DECODE(POS.PBS,1,'+',2,'-')
            ELSE POS.PFIRM||POS.POFFIC||POS.PACCT||POS.PCUSIP||DECODE(POS.PBS,1,'+',2,'-') END,POS.SYSTEMCODE,CASE WHEN POS.PSTYPE='S' THEN POS.PSYMBL ELSE POS.PFC END,POS.PEXCH||DECODE(POS.PSUBEX,'<NULL>',''),
            POS.PCURSY,
            CASE WHEN POS.PSBCUS IS NULL THEN SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) ELSE POS.PSBCUS || SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) END ,
            NVL(POS.PSUBTY,'F') ,POS.PSTRIK,*SUM(DECODE(POS.PBS,1,ABS(POS.PPRTQ),0)) ,SUM(DECODE(POS.PBS,2,ABS(POS.PPRTQ),0))* ,
            POS.PCLOSE,SUM(POS.PMKVAL) ,
            TO_CHAR(CASE WHEN INSTR(POS.PUNDCP,'.') > 0 OR LENGTH(POS.PUNDCP) < 15 THEN POS.PUNDCP ELSE TO_CHAR(TO_NUMBER(POS.PUNDCP) / 100000000) END),
            POS.UBS_ID,POS.BBG_EXCHANGE_CODE,POS.BBG_TICKER ,POS.BBG_YELLOW_KEY,POS.PPCNTY,POS.PMULTF,TO_CHAR(POS.BUSINESS_DATE,'YYYYMMDD'),
            POS.SOURCE_GMI_LIB,
            --DECODE(POS.SYSTEMCODE,'GMI1','euro','GMI2','namr','GMI3','aust','GMI4','asia','POWERBASE','aust','SINACOR','namr',POS.SYSTEMCODE),
            DECODE(p_reportid,'RVPOS_SING','euro','RVPOS_AUSTDOM','aust','RVPOS_AUSTEOD','euro','RVPOS_GLBLAPAC','asia','POS_RV_SN','namr','POS_PB','aust',POS.SYSTEMCODE),
            POS.RIC,
            CASE WHEN PSUBTY = 'S' THEN POS.TYPE ELSE NULL END,
            DECODE(POS.UBS_ID,NULL,POS.PCUSP2,POS.ISIN),POS.UNDERLYING_BBG_TICKER,POS.UNDERLYING_BBG_EXCHANGE,POS.PRODUCT_CLASSIFICATION,
            CASE WHEN PSUBTY = 'S' THEN POS.PSDSC2 ELSE NULL END,
            CASE WHEN PSUBTY = 'S' THEN C.SSDSC3 ELSE NULL END,
            NVL(C.SSECID,POS.PCUSIP),
            NULL,
            POS.PYSTMV,
            POS.PMINIT,
            POS.PEXPDT,
            CASE WHEN POS.PSUBTY='S' THEN  SUBSTR(C.ZDATA2,77,1) ELSE NULL END,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL
            FROM POSITIONS_WRK POS LEFT OUTER JOIN
            (SELECT * FROM CDS_PRODUCTS CP INNER JOIN FUTURE_MASTER FM ON
            (CP.STRXCH=FM.ZEXCH AND CP.SFC=FM.ZFC AND CP.BUSINESS_DATE = FM.BUSINESS_DATE )) C ON POS.PCUSIP = C.SCUSIP
            AND NVL(POS.PCUSP2,'X') = NVL(C.SCUSP2,'X')
            WHERE
            POS.PEXCH NOT IN ('A1','A2','A3','B1','B3','C2','D1','H1','K1','L1','M1','M3','P1','S1')
            AND (POS.PSBCUS IS NOT NULL OR POS.PCTYM IS NOT NULL OR POS.PSTYPE ='S')
            AND POS.BUSINESS_DATE = run_date_char
            GROUP BY
            POS.UBS_ID,POS.SYSTEMCODE,POS.RECIPIENTCODE,POS.BUSINESS_DATE,POS.PACCT,POS.PFIRM,POS.POFFIC,POS.PCUSIP,POS.PBS,CASE WHEN POS.PSTYPE='S' THEN POS.PSYMBL ELSE POS.PFC END,
            POS.PEXCH,POS.PSUBEX,POS.PCURSY,
            CASE WHEN POS.PSBCUS IS NULL THEN SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) ELSE POS.PSBCUS || SUBSTR(POS.PCTYM,5,2)  || SUBSTR(POS.PCTYM,1,4) END,
            NVL(POS.PSUBTY,'F') ,POS.PSTRIK,POS.PCLOSE,TO_CHAR(CASE WHEN INSTR(POS.PUNDCP,'.') > 0 OR LENGTH(POS.PUNDCP) < 15 THEN POS.PUNDCP ELSE TO_CHAR(TO_NUMBER(POS.PUNDCP) / 100000000) END),
            POS.BBG_EXCHANGE_CODE,POS.BBG_TICKER,POS.BBG_YELLOW_KEY,POS.PPCNTY,POS.PMULTF,POS.PSUBTY,POS.SOURCE_GMI_LIB,RIC,
            CASE WHEN PSUBTY = 'S' THEN POS.TYPE ELSE NULL END,
            DECODE(POS.UBS_ID,NULL,POS.PCUSP2,POS.ISIN),POS.UNDERLYING_BBG_TICKER,POS.UNDERLYING_BBG_EXCHANGE,POS.PRODUCT_CLASSIFICATION,
            CASE WHEN PSUBTY = 'S' THEN POS.PSDSC2 ELSE NULL END,
            CASE WHEN PSUBTY = 'S' THEN C.SSDSC3 ELSE NULL END,
            NVL(C.SSECID,POS.PCUSIP),
            POS.PYSTMV,
            POS.PMINIT,
            POS.PEXPDT,
            CASE WHEN PSUBTY = 'S'  THEN  SUBSTR(C.ZDATA2,77,1) ELSE NULL END;Now, could you plz help me in replacing the bold text in the query with the requirement.
    Thanks and Rgds
    DG
    Edited by: BluShadow on 16-May-2011 09:39
    added {noformat}{noformat} tags.  Please read: {message:id=9360002} for details on how to post code/data                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • About using TRIM function...

    //I'm posting this after whole search of past discussion as everybody wants to do so. :-)
    Hi,
    I want to trim out white spaces from colums in my table when select executes.
    I've read about TRIM function, but it does not as documented I think.
    See below my pseudo-code from trimming.
    SELECT trim (' ' from col1) &#0124; &#0124; col2, FROM TH_ABLICEN WHERE..blah..blah..This SQL query return me "ORA-00907: missing right parenthesis".
    I don't know how can this returns...
    TRIM function works only when its parameter is a constant and fails always when columns were injected as a parameter.
    This situation is very obscure to me and make me wondering ORACLE's intention for this kind of inconvenience.
    And I've tried replace (col1, ' ', '') only to fail.
    So my question is...
    1. How can use trim function?
    2. What is the ordinary method being used by popular demand to trim out white spaces from a column?
    Thanks in advance
    Regards,
    Ryan

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Adinath Kamode ([email protected]):
    Hello,
    Your query is correct. I tried it with 8.1.6 sql version. I modified it as follows.
    select trim(' ' FROM ENAME)| |JOB FROM EMP;
    about syntax you can specify column name in trim.
    hope reply from you.
    <HR></BLOCKQUOTE>
    As mentioned in my first posting, it returns "ORA-00907: missing right parenthesis" error.
    Anyway thanks for your concern.
    Regards,
    Ryan
    null

  • How to pass rowtype argument to a function from select statement?

    Hi all!
    I have function that takes mytable%rowtype as in parameter. can I pass entire row of mytable to the function from select statement? kind of
    select myfunction(mytable.*) from mytable where ....
    Thanks in advance

    The function can be used in a SQL statement only if it accepts SQL types and returns SQL type. %ROWTYPE being PL/SQL construct and not a SQL datatype, can not be used in this context.
    http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10743/datatype.htm#i2093

  • How to use SET ID in select statement of SQ02

    Hi,
    I have a infoset, where for one of my Zfield, i have writen a select statement in order to get my ouput.
    if aufk-aufnr = 'XYZ'.
      select sum( FKBTR ) from fmifiit into zfmifiit
      where fikrs = aufk-kokrs
      and fipex IN capex
      and wrttp NE '51'.
    endif.
    For the above statement, i have created (with 10 single values, table name FMCI, field name FIPEX)  a SET id named CAPEX and used with IN operator.
    But when i generating the infoset i am getting error like below
    The IN operator with "CAPEX" is followed neither by an internal table nor by a value list
    I was using IN opeartor for set ids during validation / substitutions, where if i have to validate multiple single values.
    What to do here?
    Regards,
    Srinu

    [OPEN-SQL|http://help.sap.com/abapdocu_70/en/ABENOPEN_SQL_GENERAL.htm] set of statement, [SELECT|http://help.sap.com/abapdocu_70/en/ABAPWHERE.htm] statement, [WHERE|http://help.sap.com/abapdocu_70/en/ABENWHERE_LOGEXP.htm] addition requires a selection table (type range, [WHERE - IN seltab |http://help.sap.com/abapdocu_70/en/ABENWHERE_LOGEXP_SELTAB.htm]) of parameter and not a set. We are in Abap here, not in a Customizing screen
    You have to convert the set to a selection table in an ABAP coding before using it in a SELECT statement.
    (Check FM like G_SET_TREE_IMPORT to import definition of set (values and intervals) and map them to a select-table, or ask a developper to perform it, look at G_SET_TREE_IMPORT -> select statement)
    Regards,
    Raymond

  • Using plsql tables in select statement of report query

    Hi
    Anyone have experience to use plsql table to select statement to create a report. In otherwords, How to run report using flat file (xx.txt) information like 10 records in flat files and use this 10 records to the report and run pdf files.
    thanks in advance
    suresh

    hi,
    u can use the utl_file package to do that using a ref cursor query in the data model and u can have this code to read data from a flat file
    declare
    ur_file utl_file.file_type;
    my_result varchar2(250);
    begin
    ur_file := UTL_FILE.FOPEN ('&directory', '&filename', 'r') ;
    utl_file.get_line(ur_file, my_result);
    dbms_output.put_line(my_result);
    utl_file.fclose(ur_file);
    end;
    make sure u have an entry in ur init.ora saying that your
    utl_file_dir = '\your directory where ur files reside'
    cheers!
    [email protected]

  • Unable to call local function in select statement.

    Hi all,
    I am unable to call the local function in select statement in below scenario.
    DECLARE
    l_cnt NUMBER;
    FUNCTION FUN1 RETURN NUMBER
    AS
    BEGIN RETURN 2;
    END;
    BEGIN
    SELECT FUN1 INTO l_cnt FROM DUAL;
    DBMS_OUTPUT.PUT_LINE(l_cnt );
    END;
    /Any alternate way to call local function in select statement?
    Thanks
    Ram.

    Hi,
    Sorry, you can't call a locally defined function in a SQL statement, even if that SQL statement is in a PL/SQL block where the function is in scope.
    This applies to packages, also. If a function is not declared in the package spec, but only in the package body, then you can't call it from SQL statements in the package body.
    Why do you want a locally defined function? Why not a function defined outside the procedure?

  • Using functions in select statement(joining 5 tables) taking long time in Oracle

    Hi,
    I have created a query in oracle which joins 5 tables and uses two functions(function names are 'ca_concat' and 'ca_concat_noseq').
    Query takes approximately 40 secs to execute around 12000 records. If I remove the functions from query it excutes within a second..
    Note : I have used the oracle SQL Developer for testing the query.
    It would be appriciated if anybody helps me to improve the perfomance of the query.
    Below are the querie with and without functions:
    1. Query with functions:
    select
    imsAuditEvent12.id as ID,
    imsAuditEvent12.audit_time as AUDIT_TIME,
    imsAuditEvent12.admin_dn as ADMIN_DN,
    imsAuditEvent12.admin_name as ADMIN_NAME,
    imsAuditEvent12.event_name as EVENT_NAME,
    imsAuditEvent12.event_description as EVENT_DESCRIPTION,
    imsAuditEvent12.event_state as EVENT_STATE,
    imsAuditEvent12.envname as ENVNAME,
    imsAuditTaskSession12.task_name as TASK_NAME,
    imsAuditTaskSession12.id as TASK_ID,
    imsAuditTaskSession12.task_description as TASK_DESCRIPTION,
    imsAuditTaskSession12.task_priority as TASK_PRIORITY,
    S1.OBJECT_ID,
    S1.OBJECT_NAME as OBJECT_NAME,
    S1.OBJECT_TYPE as OBJECT_TYPE,
    S2.ATTRIBUTE_NAME as ATTRIBUTE_NAME,
    S2.ATTRIBUTE_OLDVALUES as ATTRIBUTE_OLDVALUES,
    S2.ATTRIBUTE_NEWVALUES as ATTRIBUTE_NEWVALUES,
    S3.OBJECT_DN as OBJECT_DN,
    S3.OBJECT_TYPE as IMSOBJECT_TYPE,
    S3.CONTAINER_NAME as CONTAINER_NAME,
    S3.CONTAINER_DN as CONTAINER_DN,
    S3.CONTAINER_TYPE as CONTAINER_TYPE
    from
    imsAuditEvent12 LEFT JOIN imsAuditTaskSession12 ON imsAuditTaskSession12.id=imsAuditEvent12.tasksession_id LEFT JOIN
    (select parent_event_id,
    ca_concat('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.object_name','imsAuditEventObject12') as OBJECT_NAME,
    ca_concat('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.object_type','imsAuditEventObject12') as OBJECT_TYPE,
    ca_concat_noseq('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.ID','imsAuditEventObject12') as OBJECT_ID
    from
    imsAuditEventObject12 group by parent_event_id) S1
    ON imsAuditEvent12.id = S1.parent_event_id LEFT JOIN
    (select
    parent_object_id,
    ca_concat('parent_object_id',parent_object_id,'attribute_name','imsauditobjectattributes12') as ATTRIBUTE_NAME,
    ca_concat('parent_object_id',parent_object_id,'attribute_oldvalue','imsauditobjectattributes12') as ATTRIBUTE_OLDVALUES ,
    ca_concat('parent_object_id',parent_object_id,'attribute_newvalue','imsauditobjectattributes12') as ATTRIBUTE_NEWVALUES
    from
    imsauditobjectattributes12 group by parent_object_id) S2
    ON S1.OBJECT_ID = S2.parent_object_id LEFT JOIN
    (select
    parent_event_id,
    ca_concat('parent_event_id',parent_event_id,'OBJECT_DN','imsauditobjectrelationship12') as OBJECT_DN,
    ca_concat('parent_event_id',parent_event_id,'OBJECT_TYPE','imsauditobjectrelationship12') as OBJECT_TYPE ,
    ca_concat('parent_event_id',parent_event_id,'CONTAINER_NAME','imsauditobjectrelationship12') as CONTAINER_NAME,
    ca_concat('parent_event_id',parent_event_id,'CONTAINER_DN','imsauditobjectrelationship12') as CONTAINER_DN,
    ca_concat('parent_event_id',parent_event_id,'CONTAINER_TYPE','imsauditobjectrelationship12') as CONTAINER_TYPE
    from
    imsauditobjectrelationship12 group by parent_event_id) S3
    ON imsAuditEvent12.id =S3.parent_event_id where imsauditevent12.id > 0 and imsauditevent12.id <12000 order by imsauditevent12.id ASC;
    2. Query without using functions:
    select * from imsauditeventobject12 left join imsauditevent12 on imsauditeventobject12.id=imsauditevent12.id left join imsauditobjectattributes12 on imsauditeventobject12.id=imsauditobjectattributes12.parent_object_id left join imsaudittasksession12 on imsauditevent12.tasksession_id=imsaudittasksession12.id left join imsAuditObjectRelationship12 on imsAuditEvent12.id =imsAuditObjectRelationship12.parent_event_id where imsauditevent12.id >0 and imsauditevent12.id < 12000 order by imsauditevent12.id asc;
    Thanks,
    Badri

    Hi,
    Please find the below more information about the query.
    DB version: Oracle 11g Enterprise Edition Release 11.2.0.1.0
    Below are source of the functions:
    create or replace function ca_concat( ca_key_name in varchar2,
                           ca_key_val  in varchar2,
                           ca_other_col_name in varchar2,
                           ca_tname     in varchar2 )
       return varchar2
          as
           type rc is ref cursor;
           l_str    varchar2(32000);
           l_sep    varchar2(1);
           l_val    varchar2(32000);
           l_count   number(6);
           l_cur    rc;
       begin
       l_count :=1;
       l_str := '';
           open l_cur for 'select '|| ca_other_col_name ||'
                             from '|| ca_tname || '
                            where ' || ca_key_name || ' = '
                       using ca_key_val;
           loop
               fetch l_cur into l_val;
               l_val := SUBSTR(l_val,0,102);
               exit when (l_cur%notfound or l_count > 38);
               l_str := l_str || l_sep || l_count || '.' || l_val;
               l_sep := ',';
           l_count := l_count + 1;
           end loop;
           close l_cur;
           return l_str;
       end;
      create or replace function ca_concat_noseq( ca_key_name in varchar2,
                           ca_key_val  in varchar2,
                           ca_other_col_name in varchar2,
                           ca_tname     in varchar2 )
       return varchar2
          as
           type rc is ref cursor;
           l_str    nvarchar2(32000);
           l_sep    varchar2(1);
           l_val    varchar2(32000);
           l_count   number(6);
           l_cur    rc;
       begin
       l_count :=1;
           open l_cur for 'select '||ca_other_col_name||'
                             from '|| ca_tname || '
                            where ' || ca_key_name || ' = '
                       using ca_key_val;
           loop
               fetch l_cur into l_val;
               exit when (l_cur%notfound or length(l_val)>3000 or l_count>1);
               l_str := l_str || l_sep || l_val ;
               l_sep := ',';
           l_count := l_count + 1;
           end loop;
           close l_cur;
           return l_str;
    end;
    Below are the tables structures:
    DESC imsauditevent12;
    Name                           Null     Type                                                                                                                                                                                      
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    TASKSESSION_ID                 NOT NULL NUMBER                                                                                                                                                                                       
    TASKSESSION_OID                         VARCHAR2(100)                                                                                                                                                                                
    PARENT_EVENT_OID                        VARCHAR2(100)                                                                                                                                                                                
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    EVENT_OID                      NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ADMIN_DN                       NOT NULL VARCHAR2(512)                                                                                                                                                                                
    ADMIN_NAME                              VARCHAR2(255)                                                                                                                                                                                
    EVENT_NAME                     NOT NULL VARCHAR2(255)                                                                                                                                                                                
    EVENT_DESCRIPTION                       VARCHAR2(4000)                                                                                                                                                                               
    EVENT_STATE                             VARCHAR2(100)                                                                                                                                                                                
    ENVNAME                        NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ENV_OID                        NOT NULL VARCHAR2(100)                                                                                                                                                                                
    DESC imsauditeventobject12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_EVENT_ID                NOT NULL NUMBER                                                                                                                                                                                       
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    OBJECT_TYPE                    NOT NULL VARCHAR2(100)                                                                                                                                                                                
    OBJECT_NAME                             VARCHAR2(255)                                                                                                                                                                                
    DESC imsauditobjectattributes12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_OBJECT_ID               NOT NULL NUMBER                                                                                                                                                                                       
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    DISPLAY_NAME                            VARCHAR2(255)                                                                                                                                                                                
    ATTRIBUTE_NAME                 NOT NULL VARCHAR2(255)                                                                                                                                                                                
    ATTRIBUTE_OLDVALUE                      VARCHAR2(4000)                                                                                                                                                                               
    ATTRIBUTE_NEWVALUE                      VARCHAR2(4000)                                                                                                                                                                               
    DESC imsaudittasksession12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_TS_OID                           VARCHAR2(100)                                                                                                                                                                                
    PARENT_EVENT_OID                        VARCHAR2(100)                                                                                                                                                                                
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    TASKSESSION_OID                NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ADMIN_DN                       NOT NULL VARCHAR2(512)                                                                                                                                                                                
    ADMIN_NAME                              VARCHAR2(255)                                                                                                                                                                                
    TASK_NAME                               VARCHAR2(255)                                                                                                                                                                                
    TASK_TAG                       NOT NULL VARCHAR2(255)                                                                                                                                                                                
    TASK_DESCRIPTION                        VARCHAR2(4000)                                                                                                                                                                               
    TASK_PRIORITY                           NUMBER                                                                                                                                                                                       
    STATE                          NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ENVNAME                        NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ENV_OID                        NOT NULL VARCHAR2(100)                                                                                                                                                                
    DESC imsAuditObjectRelationship12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_EVENT_ID                NOT NULL NUMBER                                                                                                                                                                                       
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    OBJECT_TYPE                    NOT NULL VARCHAR2(100)                                                                                                                                                                                
    OBJECT_DN                      NOT NULL VARCHAR2(512)                                                                                                                                                                                
    CONTAINER_TYPE                 NOT NULL VARCHAR2(100)                                                                                                                                                                                
    OBJECT_NAME                    NOT NULL VARCHAR2(255)                                                                                                                                                                                
    CONTAINER_NAME                 NOT NULL VARCHAR2(255)                                                                                                                                                                                
    CONTAINER_DN                   NOT NULL VARCHAR2(512)                                                                                                                                                                          
    OPERATION                      NOT NULL VARCHAR2(50)                                                                                                                                                                                 
    Thanks,
    Badri

  • Using Column Name returned by function in SELECT statement

    Hi
    Output from my function (RETURN data type is VARCHAR2) is column name. I want to use it directly in my SELECT statement. Below is simplified example of this:
    --- Function
    CREATE OR REPLACE FUNCTION simple RETURN varchar2 IS
    BEGIN
    RETURN ‘my_column’;
    END simple;
    --- Select
    SELECT simple FROM my_table;
    This does not work. It seems that output from function is passed in quotation i.e.
    SELECT ‘my_column’ FROM my_table;
    So the output from SELECT is a list of rows populated with values my_table:
    COLUMN     simple
    ROW1     my_column
    ROW2     my_column
    ROW3     my_column
    Can please someone help me with this?

    I'm not sure I got you right.
    In standard SQL everything must be known at compile time. If not dynamic SQL is required, but is a costly operation (usually requires parsing before each execution) so it should better not be used when standard SQL can do it.
    I provided a design time example where a function returns the column name from the given the table name and column id for a varchar2 column data type to make things simple. Then a query string is constructed and executed dynymically to return all column values of the chosen table_name.column_name.
    SELECT simple FROM my_tableAt compile time the simple function return value is unknown (any varchar2 value would do) you already find out you get the (same) return value (i.e column name) for each table row => dynamic SQL needed to get the column values
    The purpose of function would be to rename all columns for provided table.The table name would be provided, right? If yes => dynamic SQL
    What is the function supposed to return the column name (where would the new name come from?), the column alias (which table column would be renamed to the new name?)
    The user could use the new_column name as the column alias name submitting the query.
    Is it possible to do this?Maybe () using a pipelined function (different data types - number,date, ... not cosidered yet) but your simple query;
    <tt>SELECT simple FROM my_table</tt>
    might look like:
    <tt>select my_column_value new_column_name from table(get_column_value(table_name,column_name))</tt>
    Sorry, no Database at hand to provide a specific example.
    Regards
    Etbin

Maybe you are looking for