Decode or better function

Hi
I need to do select with the following logic
if end_date > sysdate or null - need to display active
if end_date <= sysdate - not active
Thanks
anna

SQL> select serial_no,
  2         name,
  3         end_date,
  4         case
  5           when end_date is not null and end_date <= sysdate then
  6            'not active'
  7           else
  8            'active'
  9         end active_or_not
10    from test
11  ;
SERIAL_NO NAME                           END_DATE  ACTIVE_OR_
         1 Smith                          01-JUN-11 not active
         2 Smith                          01-JUL-11 not active
         3 John                                     active

Similar Messages

  • Which is better Function modules or Include programs?

    Hi,
    I am working on an enhancement and it has lot of screens with a tree structure on the left. Now we are planning to have each screen to have its PAI/PBO and the processing logic to be in seperate include programs.
    However in our team we have debate as to go in for include programs or for Function modules. The opinion here is that the program should not take long time to load if its going to be include programs.
    Please suggest what approach is better, is it Function modules or include programs.
    Any other suggestion is also welcome.
    Thank You,
    SAC.

    Thank You all.
    In this enhancement I have about 13 screens and has business logic in each. So if I go with each screens and business logic coding in seperate Function modules than includes under 1 function group is adviceable?
    Using object oriented approach is really the best thing here, but however most of my team members are not welversed in OO concepts. The deadlines are very tight for grooming my team members as well.
    My only concern is that with so much screens and business logic in 1 transaction should not bring down this programs performance when its executed.
    Any other suggestions to keep in mind for this is appreciated.
    Thank You.
    SAC

  • Decode null with function call

    Hi
    Is it possible to use decode in select for the below scenario, if so what is the correct syntax
    IF function one returns null I want to call display the value from function 2 in decode statement.
    Regards
    anna

    Hi, Anna,
    I would use COALESCE for that:
    COALESCE (a1, a2, a3, ...)returns the first of its arguments (there can be as few as 2, and probably at least 100) that is not NULL.
    If you need help, post some sample data (CREATE TABLE and INSERT statements), function code, and the results that you want from that data.

  • How to use decode or case function in this?

    Hi,
    I want to implement this condition in a query without using any user defined functions
    1.if (T_fees = 'ABC' then if (p_fees>0) then nvl(p_fees,0) else (p_fees + a_fees) else 0)
    2. if(g_fees <> (if t_fees = 'ABC' then if (p_fees>0) then nvl(p_fees,0) else (p_fees)
    else 0
    Is it possible to use any nested 'CASE' statement ?
    This is very urgent...
    Regards,
    Shilpa

    Hi,
    Is it possible to use any nested 'CASE' statement ?Yes it it possible.
    "afiedt.buf" 7 lines, 89 characters
      1  SELECT sysdate FROM DUAL
      2  WHERE 1 = (CASE WHEN 1=1  AND 2=2 THEN
      3             1
      4       ELSE
      5             2
      6*     END)
    SQL>/
    SYSDATE
    07-AUG-06
    1 row selected.
    "afiedt.buf" 11 lines, 139 characters
      1  SELECT sysdate FROM DUAL
      2  WHERE 1 = (CASE WHEN 1=1  AND 2=2 THEN
      3             CASE WHEN 3=3 AND 4=4 THEN
      4                     1
      5             ELSE
      6                     0
      7             END
      8       ELSE
      9             0
    10*     END)
    SQL>/
    SYSDATE
    07-AUG-06
    1 row selected.
    You can implement your logic.
    Regards

  • Function or pseudo-column 'DECODE' may be used inside a SQL statement only

    Hi everyone!
    I got the error in the subject concernig the follow piece of sql code
    + v_str_sql_body :=
    v_str_sql_body || ' and cod_entita ='
    || decode(cur.cod_entita_a,str_all,cur_ent.cod_entita,cur.cod_entita_a);
    execute immediate v_str_sql_body; +
    I can't understand what's the problem with it.
    Can anyone help me to find the error?
    Thank you in advance!
    Samuel
    Edited by: 996508 on 27-mar-2013 5.29

    Hi, Samuel,
    Welcome to the forum!
    996508 wrote:
    Hi everyone!
    I got the error in the subject concernig the follow piece of sql code
    + v_str_sql_body :=
    v_str_sql_body || ' and cod_entita ='
    || decode(cur.cod_entita_a,str_all,cur_ent.cod_entita,cur.cod_entita_a);
    execute immediate v_str_sql_body; +
    I can't understand what's the problem with it.Sorry, I can't understand what you're trying to do.
    Whenever you have a problem, post a complete script that people can run to re-create the problem and test their ideas. Include your complete PL/SQL code (if the problem involves PL/SQL) including code to call your procedure, a little sample data (CREATE TABLE and INSERT statements) for any tables involved, and the results you want the given sample data.
    Always say which version of Oracle you have (e.g., 11.2.0.3.0).
    See the forum FAQ {message:id=9360003}
    Can anyone help me to find the error?As the error message says, you can only use DECODE in a SQL statement. That is:
    v_str_body := v_str_body
            || ' and cod_entita = '
            || DECODE (...);is always an error, no matter what ... stands for.
    Perhaps you meant to have the DECODE inside the string literal.
    If not, then (depending on your version) you can use CASE instead of DECODE:
    v_str_body := v_str_body
            || ' and cod_entita = '
            || CASE  cur.cod_entita_a
                     WHEN  str_all  THEN  cur_ent.cod_entita
                                 ELSE  cur.cod_entita_a
               END;Anything that DECODE can do, CASE can do, too.
    DECODE is sometimes a little simpler than CASE to use, but only a little. It is never faster than CASE.
    CASE is sometimes much, much simpler to use than DECODE, and sometimes much faster, too.
    I won't say never use DECODE; but I will say rarely use DECODE. Use CASE instead, unless you have a specific reason why DECODE is better in that particular situation.
    In PL/SQL, you can also use IF ... THEN ... ELSE in places where you might want to use DECODE or CASE.
    For example:
    v_str_body := v_str_body
            || ' and cod_entita = ';
    IF  cur.cod_entita_a = str_all
    THEN
        v_str_body := v_str_body
                    || cur_ent.cod_entita;
    ELSE
        v_str_body := v_str_body
                    || cur.cod_entita_a;
    END IF;Edited by: Frank Kulash on Mar 27, 2013 5:59 PM
    Added IF ... THEN ... ELSE example.

  • Decode or nvl2 or case functions

    create a query that displays employees lastname and commission amounts.if an employee does not earn commission,put "no commission."label column comm
    Sample Output
    lastname comm
    king no commission
    abel .3
    Note:The column commission is of number datatype

    Hi,
    This looks an awful lot like homework.
    If you're really stuck, post your best attempt so far, and a description of what you think is wrong with it.
    The assignment itself, "Decode or nvl2 or case" gives you three different ways to do this.
    CASE is a lot more useful in general, so I suggest learning how to use it first.
    NVL2 is a little more concise for this particular problem.
    DECODE can do anything CASE can do, often with a lot more coding. Unless it's required for your schiool work, I don't suggest using DECODE.
    You'll probably need to use the TO_CHAR function, to convert the commission to a string, so that whatever kind of expression you use (CASE or NVL2 or DECODE) it will always return the same datatype: VARCHAR2.
    All built-in functions are documented in the SQL Language manual:
    http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/index.htm
    Actually, CASE and DECODE are not functions, but they're described in the same manual,.

  • DECODE function with ' ' and ' '

    I need of a command to compare intervals of dates, similar to DECODE, only that using '<' and '>'.
    Example: if the date will be minor who ' 2005-10-01 ' then return ' problem ', else if the date will be minor who 2005-11-01 and greater that 2005-10-01, then returns ' ok '
    I tried to use the function ' decode ' with the function ' sign ', but this only functions for numbers and it does not stop dates.
    Former: Using a column that contains numbers, ok.
    SELECT decode(SIGN(col_number-10),-1,'<10',decode(SIGN(col_number-20),-1,'<20',decode(SIGN(col_number-30),-1,'<30'))) col_number
    FROM table
    If to use a column that contains dates (dates or timestamp), problem (types of incosistentes dates: it waited NUMBER got YOU DATE).
    SELECT decode(SIGN(col_date-10),-1,'<10',decode(SIGN(col_date-20),-1,'<20',decode(SIGN(col_date-30),-1,'<30'))) col_date
    FROM table
    Somebody has some idea?

    Somebody has some idea?Yes, CASE().
    Cheers, APC

  • Decode & transformation function

    functions our mapping between source table and target IT table in staging area, we have lots of complex Decodes. Both target and source are Oracle 8i databases.
    OWB does not support Decode in it's expression in mapping. I understand that workaround is to use transformation operator - that in turn creates a database function. My questions are as below:
    (1) Due to may column to column mappings using Decode, I will end up with about 30- 40 functions for each fact due to this approach. Isn't that way too many individual functions cluttered in the database? Any pointers to streamlining this into one package of funtions - how do you do it in OWB?
    (2) Which version of OWB onwards is there support for Decode?
    Thanks

    Hi,
    First let me explain what the issue is with Decode, OWB generates two types of code (set based and row based) in one package. They are always there. Decode however does not work in row based context as OWB tries to do something like this:
    if decode(ijfijroje) then
    which will not work and not compile. Therefore we recommend to use CASE which as of Oracle 9i compiles in both SQL as in PL/SQL contexts.
    In your context the only thing you can do is wrap decode in a function. To be quite honest other then having a lot of them the only thing I think you can do is create a package and call the function that way. Then they are nicely wrapped in a single component.
    Hope this helps,
    Jean-Pierre

  • Case and decode - nested functions

    Can i use a case statement inside a decode or nvl2 function?
    eg.
    decode(col_a,'ABC',
    case when col_b in (1,2) then 'CCC' else when col_b in (3,4,5) then 'XXX' end
    , 'NA')
    Can i use a function like this basically, can i nest a case statement within a decode or nvl2 function?
    I have used nested decodes and nvls and nested case statements but not combined both so far.... so need an opinion...
    Thanks.
    Edited by: user254668 on Apr 30, 2010 1:35 PM

    user254668 wrote:
    Can i use a case statement inside a decode or nvl2 function? Absolutely. Just get rid of that else:
    decode(col_a,'ABC',
    case when col_b in (1,2) then 'CCC' when col_b in (3,4,5) then 'XXX' end
    , 'NA') SY.

  • Decode in pl/sql

    Hi all
    I read that in order to use the DECODE function in PL/SQL block you will have to use the system table dual.
    my query is why we cannot use user table like emp while using decode function???

    thois might give some help,
    in general use dual, if the function used in the select list is independent of any data of a particular table, like emp in your case, check this out,
    select decode('',null,'is null','is not null') from dual
    will return is null
    but
    select decode('',null,'is null',' is not null') from emp
    will work and will give the same result but will give many records as of emp table, so either a distinct can be used or row_num = 1 can be used, but the whole point to to get things done with little cost, so better use dual for such tables...
    answering this query, i have got few doubts, decode() is a fuunction which does the selection operation, but
    1) how to implement a custom function with variable number of parameters
    2) as decode is a function which returns one of the input parameter on execution, why can't we call
    x := decode('',null,'is null','is not null');
    kindly reply to the above two queries???
    cheere

  • PLS help: animate gif decoding problem

    i am making a gif animation decoders
    using http://www.fmsware.com/stuff/gif.html
    after i decoded the gif, i could only view the animation by ACD See, but cant view by web browser(it only show the last frame of the gif) !!
    Thanks all you guy ~~~

    It seems this animated gif decoder doesn't function properly. Perhaps, you may use Jimi (http://java.sun.com/products/jimi/) to decode animated gif image :
    Enumeration images=Jimi.createJimiReader(inputStream, Jimi.SYNCHRONOUS).getImageEnumeration();

  • To generate sql string of desired row  using function.

    Hi l,
    I return the below query to get sum of vehicles which falls between the specific weight ranges.
    It returns 20 rows( As I selected 4 classification vehicles).I was expect only 4 rows .One row for each classification of vehicles.
    Is there a way if do some loop or decode in my function logic?
    will tht work?
    "(This is only smple. In my real appilication, if I use 15 classification vehicles it returns 320 rows. But i expect only 16 rows - 0 to 15 one row for each classification .Duirng runtime I'm getting an error as buffer string too small)"
    How could I solve this?
    I use this query inside a function and I call that function in a dynamic sql.
    SELECT 'SUM('
        || 'CASE '
        || 'WHEN weight_report_data.weight >=  '|| repo_range_param.min_value ||' 
            AND weight_report_data.weight   <       repo_range_param.max_value
            THEN weight_report_data.weight_count '   
        || 'ELSE 0 '
        || 'END '
        || ') "Class ' || repo_param.r_value || '" '   
          FROM repo_param
          JOIN repo_range_param
            ON repo_param.r_id = repo_range_param.r_id
         WHERE repo_param.r_id    = 1
           AND repo_param.r_group = 'class'
           AND repo_param.r_name  = 'class'
           AND repo_range_param.r_group = 'gvw'
           AND repo_range_param.r_name  = 'gvw'
         ORDER BY CAST(repo_param.r_value AS NUMBER) ASC;
    "sample sql string output when I run the above query "
    SUM(CASE WHEN weight_report_data.weight >=10
             AND weight_report_data.weight  <15
            THEN weight_report_data.weight_count ELSE 0 END ) "Class 0"
    SUM(CASE WHEN weight_report_data.weight >=15
             AND weight_report_data.weight  <20
            THEN weight_report_data.weight_count ELSE 0 END ) "Class 1"
    SUM(CASE WHEN weight_report_data.weight >=5
             AND weight_report_data.weight  <10
            THEN weight_report_data.weight_count ELSE 0 END ) "Class 2"
    SUM(CASE WHEN weight_report_data.weight >=20
             AND weight_report_data.weight  <25
            THEN weight_report_data.weight_count ELSE 0 END ) "Class 3"
    "sample data for the tables involved"
    CREATE  TABLE weight_report_data
    start_date_time       TIMESTAMP      NOT NULL,
    end_date_time         TIMESTAMP      NOT NULL,
    weight                NUMBER(12,0)   NOT NULL,
    weight_count          NUMBER(12,0)   NOT NULL
    INSERT INTO weight_report_data(start_date_time, end_date_time, weight, weight_count)
           VALUES('01-JUL-08 12:00:00','03-JUL-08 12:00:00',4,5);
    INSERT INTO weight_report_data(start_date_time, end_date_time, weight, weight_count)
           VALUES('01-JUL-08 12:00:00','03-JUL-08 12:00:00',3,1);
    INSERT INTO weight_report_data(start_date_time, end_date_time, weight, weight_count)
           VALUES('01-JUL-08 12:00:00','03-JUL-08 12:00:00',8,6);
    INSERT INTO weight_report_data(start_date_time, end_date_time, weight, weight_count)
           VALUES('01-JUL-08 12:00:00','03-JUL-08 12:00:00',25,9);
    CREATE  TABLE repo_param
    R_ID       NUMBER(12,0),
    R_GROUP     VARCHAR2(10),
    R_NAME     VARCHAR2(10),
    R_VALUE     VARCHAR2(10)
    INSERT INTO repo_param(r_id, r_group, r_name, r_value)
           VALUES(1,'class','class',0);
    INSERT INTO repo_param(r_id, r_group, r_name, r_value)
           VALUES(1,'class','class',1);
    INSERT INTO repo_param(r_id, r_group, r_name, r_value)
           VALUES(1,'class','class',2);
    INSERT INTO repo_param(r_id, r_group, r_name, r_value)
           VALUES(1,'class','class',3);
    CREATE  TABLE repo_range_param
    R_ID       NUMBER(12,0),
    R_GROUP     VARCHAR2(10),
    R_NAME     VARCHAR2(10),
    MIN_VALUE NUMBER(3,0),
    MAX_VALUE     NUMBER(3,0)
    INSERT INTO repo_range_param(r_id, r_group, r_name, min_value, max_value)
           VALUES(1,'gvw','gvw',0,5);
    INSERT INTO repo_range_param(r_id, r_group, r_name, min_value, max_value)
           VALUES(1,'gvw','gvw',5,10);
    INSERT INTO repo_range_param(r_id, r_group, r_name, min_value, max_value)
           VALUES(1,'gvw','gvw',10,15);
    INSERT INTO repo_range_param(r_id, r_group, r_name, min_value, max_value)
           VALUES(1,'gvw','gvw',15,20);
           INSERT INTO repo_range_param(r_id, r_group, r_name, min_value, max_value)
           VALUES(1,'gvw','gvw',20,25);
    Thanks in advance
    Edited by: user10641405 on Jun 18, 2009 4:10 PM
    Edited by: user10641405 on Jun 18, 2009 4:15 PM
    Edited by: user10641405 on Jun 18, 2009 4:15 PM
    Edited by: user10641405 on Jun 18, 2009 4:16 PM
    Edited by: user10641405 on Jun 18, 2009 4:17 PM
    Edited by: user10641405 on Jun 18, 2009 7:47 PM
    Edited by: user10641405 on Jun 19, 2009 4:15 PM

    Say you have a 3 tables
    Table name Description rowcount()
    ============================================================================
    weight_report_data report table having weights and counts 4
    repo_param having id column gives different classes 4
    repo_range_param is detail table of repo_param with id 4
              as foreign key and defines weight
              min max ranges.
    Now you need to report
    per class how many weights weights are there ?
    --not understood report id clearly in relation to weight_report_data
    step1:
    need to map each row of weight_report_data againist its class.
    select wrd.weight,wrd.weight_count,(select rp.class||rp.value
    from repo_range_param rrp,
              repo_param rp
    where rrp.id= rp.id
    and rp.id=1 -- add your other join condition
    and wrd.weight between rrp.min and rrp.max ) class_nm
    from weight_report_data wrd ;
    This will give 4 rows , i.e for each row in wrd it tells which class it falls.
    now you want to group by class_nm to get sum or count of weights.
    step2:
    select class_nm,sum(weight), count(weight), sum(weight_count)
    from (
    select wrd.weight,wrd.weight_count,(select rp.class||rp.value
    from repo_range_param rrp,
              repo_param rp
    where rrp.id= rp.id
    and rp.id=1 -- add your other join condition
    and wrd.weight between rrp.min and rrp.max ) class_nm
    from weight_report_data wrd ) wrd_classnm
    group by class_nm;
    I hope this helps .
    suggestion: first analyse and breakdown into stpes how you r going to do if you do by hand and then translate into sql and then optimise

  • (How) Can a SP call the Function which has BULK COLLECT return?

    I have a function using BULL COLLECT to put (XML data) many line into a table type record. (I had this as a function because it's gong to call many times).
    From one SP program, I will need to call it to get converted XML data info, how should i call it? If it's single line return, I could use
    my_result := get_tbl_xml(p_xml);
    but when it's table type, even I defined it, it still get error. Here is the program:
    Declare (at package spec)
    TYPE OrderDtl_Tab IS TABLE OF xxx%ROWTYPE INDEX BY BINARY_INTEGER;
    in_OrderDtlTab OrderDtl_Tab;
    FUNCTION get_tbl_xml (p_xml IN CLOB ) RETURN OrderDtl_Tab IS
    BEGIN
    SELECT extractvalue(VALUE(x), '/xxxx/xxxx') AS xxxx BULK COLLECT
    INTO in_OrderDtlTab
    FROM TABLE(xmlsequence(extract(xmltype(p_xml), '/Order/Lines/*'))) x;
    RETURN in_OrderDtlTab;
    END;
    PROCEDURE SAVE_A (p_xml IN CLOB )
    BEGIN
    --- how to call get_tbl_xml to get result, so I will be able to insert into DB table
    in_OrderDtlTab := get_tbl_xml(p_xml);
    END;
    --------------------------------------------------------------------------------------------------------------------

    May be this is better
    FUNCTION get_tbl_xml (p_xml IN CLOB ) RETURN OrderDtl_Tab IS
    BEGIN
    SELECT extractvalue(VALUE(x), '/xxxx/xxxx') AS xxxx BULK COLLECT
    INTO in_OrderDtlTab
    FROM TABLE(xmlsequence(extract(xmltype(p_xml), '/Order/Lines/*'))) x;
    RETURN in_OrderDtlTab;
    END;
    PROCEDURE SAVE_A (p_xml IN CLOB )
    yourrow your_table%rowtype;
    BEGIN
    --- how to call get_tbl_xml to get result, so I will be able to insert into DB table
    in_OrderDtlTab := get_tbl_xml(p_xml);
    FORALL myrowtype
    IN in_OrderDtlTab .FIRST .. in_OrderDtlTab .LAST
    INSERT INTO Your_table
    VALUES in_OrderDtlTab(yourrow);
    END;
    END;

  • Problem loading images from a return value of a function

    hello, I write because I uin problem in loading images from a return value of a function.
    I created a database with a field "image" of type string, where I put the physical address of the image. I have written like this: {__DIR__ }foto.jpg
    Place the code, so you understand better
    function imageViewImage(): javafx.scene.image.Image {
    connetti();
    lass.forName(driverName);
    con = DriverManager.getConnection(url,user,"");
    stmt = con.createStatement();
    var richiesta:String = "SELECT image from viaggio WHERE id_viaggio=7";
    rs1 = stmt.executeQuery(richiesta);
    var result :String;
    rs1.next();
    risultato = rs1.getString("image");
    JOptionPane.showMessageDialog(null, result);
    var imagez = Image{
    url:"{result}";
    return imagez;
    The image is in the source code package
    First I connect to the database, run the query and I take the contents of the image.
    Can anyone help me?
    Is right to put in the database {__DIR__} foto.jpg or do I put only foto.jpg?

    Hello unkus_nob,
    I would rather suggest you to save only filename of that image like "foto.jpg" in database. And just concat it with the String variable containing "{__DIR__}".
    Actually the javafx compiler converts the {__DIR__} to the existing class directory path something like : "jar:file:/..../".
    var currentDir ="{__DIR__}";
    risultato = "{currentDir}{rs1.getString("immagine"})"; 
    var image = Image{
        url:risultato
    return image;--NARAYAN G.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • SQL function help

    Hi, I am trying to write a sql query to replace the string starting with '<' with NULL. Right now I am using CASE statement to replace with NULLs. Is there any function to replace with NULLs for all the strings starting with '<%'.
    Example:
    CASE When field_name LIKE '<%' then NULL else field_name
    (before) After
    <NA> NULL
    <UK> NULL
    I want to replace the above sql suery with a single sql function without using CASE statements.
    I tried to use REPLACE function, but it replaces only single character. I want to replace the whole string
    Thanks in advance.

    Hi,
    Adam Martin posted a good solution. I hope that it is clear to you that you have to replace the string '<NA>' with your column(s).
    I.e.:
    SELECT REGEXP_REPLACE (a.col1, '^<.?+', '') AS col1
         , REGEXP_REPLACE (a.col2, '^<.?+', '') AS col2
      FROM table1 a;
    {code}
    You can use also the DECODE and SUBSTR functions to have the same result:
    The code below will get the first character with SUBSTR function and the DECODE function will return NULL if the value of the first character is "<" otherwise the value of the column.
    {code:sql}
    SELECT DECODE(SUBSTR(a.col1,1,1),'<', NULL, a.col1) AS col1
         , DECODE(SUBSTR(a.col2,1,1),'<', NULL, a.col2) AS col2
      FROM table1 a;
    {code}
    but using REGEXP_REPLACE is easier to write.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Maybe you are looking for