Conditional replace Function

Good afternoon everyone,

I am trying to assign which I am certain is a basic replace function on certain values in the first column of a table depends on the value of the second column. So if I run the SELECT simple below I get the following:

-SCRIPT-

SELECT Col_name_1, Col_name_2

Table_Name FROM

-Results-

Col_name_1 Col_name_2

13                    0

23                    0

44                    1

47                    1

50                    0

Now, say I wanted to replace the values of "Col_name_1" for negative values if the value in the same line of "Col_name_2" is 1. Which gives the following result.

Col_name_1 Col_name_2

13                    0

23                    0

-44                    1

-47                    1

50                    0


, What would be the most effective way to do it. I thought using logic in the sense of if (Col_name_2 = 1), and then replace (col_name_1, '-&col_name_1') the syntax of this is obviously incorrect, I was wondering how I could produce a "function to replace conditional" which affect only certain rows in the table of results depends on the State.

Thank you very much

Charlie

Hello

7eb33dbf-5ba7-4248-8088-339e77b11c5e wrote:

Hi, thanks for you far pointers. Some real examples of data. Col_name_1 was actually 'TARGKEY' and Col_name_2 'CHANGESIGN' then the digital operations to find the reverse (*-1) won't work.

I guess that means I'm trying to add a hyphen to the TARGKEY value indicates if the value of CHANGESIGN is 1

To clarify, yes I'm trying only to change the values in the table of results and not "up-to-date" the table of origin.

It comes to my script.

SELECT TARGKEY, CHANGESIGN

OF FDM_SAB. TDATAMAP

TARGKEY CHANGESIGN
P0126010 1
P0126015 1
P0126015 1
P0126015 1
P0126015 1
P0126015 1
P0126015 1
P0126015 1
P0126015 1
1078 0
1078 1
1078 0
[NONE] 0

Once more, always post CREATE TABLE and INSERT statements for the sample data and show the exact results you want from this data.

If targkey is a string, you can prepend a hyphen with the |   operator, like this:

SELECT THE CHECK BOX

WHEN changesign = 1

THEN '-' | targkey

Of OTHER targkey

END AS new_targkey

changesign

OF tdatamap

;

It is preceded by a hyphen to targkey when changesign = 1.  That is what you want, or there are a few exceptions (for example, targekey already begins with a hyphen or is null)?  If there is no special cases like this one, include examples in the sample data, results and explanations.

Tags: Database

Similar Questions

  • Need help to create a conditional LOV function

    First off the coast to let put me the warning that I am not the right Wick informed in PL/SQL. I need to use the LOV conditional for a list of selection based on the user's role in this application.

    I use the example of samples of the Dene - link: [http://apex.oracle.com/pls/otn/f?p=31517:120:2954663700615140:NO]

    I created the 2 types required and tested the function with a simple select query and LOV works.

    However, I need the function to return values based on the user role. I know, it's probably the ugliest pl/sql, you may have seen yet ;) I hope that you guys can decipher the logic that I use. Obviously, this code does not validate.

    You guys could help me with this?
    CREATE OR REPLACE FUNCTION return_art_lov_fn
    RETURN art_table_type
    AS
      v_data   art_table_type := art_table_type ();
    BEGIN
    IF
    EXISTS (SELECT '1'FROM GBL_PEOPLE, GBL_ACCESS WHERE upper(gbl_people.userid) = upper(app_user) AND gbl_people.person_id = gbl_access.person_id and gbl_access.art_role = 9)
    THEN
    FOR c IN (SELECT reverse_name, person_id
                   FROM gbl_people)
       LOOP
          v_data.EXTEND;
          v_data (v_data.COUNT) := art_rectype (c.person_id, c.reverse_name);
       END LOOP;
    ELSE
    FOR c IN (select reverse_name,person_id from GBL_PEOPLE where upper(userid) = upper(APP_USER) and current_flag is not null
               union
              select reverse_name,person_id from GBL_PEOPLE where mgr_person_id =(select person_id from GBL_PEOPLE where upper(userid) = upper(APP_USER) and
               current_flag is not null) and current_flag is not null)
       LOOP
          v_data.EXTEND;
          v_data (v_data.COUNT) := art_rectype (c.person_id, c.reverse_name);
       END LOOP;
    ENDIF;
       RETURN v_data;
    END;
    -Vinod

    Published by: user4908943 on December 10, 2009 16:37

    Published by: user4908943 on December 10, 2009 16:40

    Published by: user4908943 on December 10, 2009 16:41

    Published by: Jeremy on December 11, 2009 04:34
  • MacBook Pro (mid-2012) retina.  Battery message "Condition: replace now!  What needs to be replaced and what should be the cost?

    I live in South Africa and have one of the retina MacBook Pro 15 "(mid - 2012).  A message appeared recently advising "Condition: replace now."  We approached Apple in Cape Town (South Africa) providers and received two different answers on what needs to be repaired and what would be the cost.  First of all, we were told that battery and top case had to be replaced at a cost of 5268.00 South African Rand.  When you take the computer repair, told that the keyboard was also being replaced with the battery and the top box, and that the price had esculated to South African Rand 12600.00 (for the part or parts only).

    Could we inform you on what needs to be repaired/replaced in such a circumstance? And the planned price range should be for repairs/parts.

    Thank you!!!

    They gave a reason for the top box of replacement?

    For info.

    https://support.Apple.com/Mac-notebooks/repair/service/pricing

    https://support.Apple.com/Mac-notebooks/repair/service

    Click the icon of American flag at the bottom right of the page and select the page that opens in South Africa

    for specific information of the South Africa.

  • When I check my battery icon it says, ' Condition: replace soon "why?

    Recently my 2010 Macbook Pro battery icon when you press says "Condition: replace soon" why?

    The most obvious answer would be that your battery reaches the end of its useful lifetime.

    If this original battery (which would be about five years old today)?

  • Create Or Replace function error

    Hello

    I do the search of texts using Oracle SQL Developer: ODMiner... I imported the "WEBLOG" data into a table... These weblog data consists of the activity of users, date, time, url, etc. The first step I took was to use a function to convert the date and time I have in the data table, in a number representing the 40 minutes since 01/01/1990. I did it by dividing by 2400 (seconds in 40 minutes). The main objective is to have a period of time for the sessions.
    I used the following code,

    (SsnDate) CREATE or REPLACE FUNCTION
    DEFAULT VARCHAR2 03/01/18 EFFECTIVE DATE,
    TIME IN VARCHAR2
    ) RETURN NUMBER
    AS
    BEGIN
    RETURN TRUNC ((to_date(DATE||) e '|| TIME, ' Hh: mm: DD-MM-YY ')-to_date('01-JAN-1990','DD-MON-YYYY')) * (86400/2400);
    END ssnDate;

    This is what appeared in the newspaper after the execution of the statement,

    SsnDate compiled FUNCTION
    WARNING: the execution is completed with warning

    After that, I tried to create a VIEW to change the DATE and TIME with the ssnDate that was created earlier and concatenate the CS_URI_STEM (which is the accessible resource) and CS_URI_QUERY (which is the query, if any, the client was trying to perform) in a new field called WEB_LINK.

    This is the code used

    CREATE OR REPLACE VIEW WEBLOG_VIEWS ("C_IP", "WEB_LINK", "CS_USER_AGENT", "SESSION")
    AS
    SELECT ssnDate (LOG_DATE, LOG_TIME) AS "SESSION."
    C_IP,
    CS_USER_AGENT,
    (CS_URI_STEM |'?) ' || CS_URI_QUERY) AS WEB_LINK
    OF THE WEB BLOG.

    Now that I got the following error...

    Error from the 1 in the command line:
    CREATE OR REPLACE VIEW WEBLOG_VIEWS ("C_IP", "WEB_LINK", "CS_USER_AGENT", "SESSION")
    AS
    SELECT ssnDate (LOG_DATE, LOG_TIME) AS "SESSION."
    C_IP,
    CS_USER_AGENT,
    (CS_URI_STEM |'?) ' || CS_URI_QUERY) AS WEB_LINK
    WEBLOG
    Error in the command line: 3 column: 38
    Error report:
    SQL error: ORA-00923: KEYWORD not found where expected
    00923 00000 - "FROM keyword not found where expected"
    * Cause:
    * Action:

    I do not understand where I am going wrong with this... It comes to the data preparation step that requires me to prepare the data before applying the techniques of modeling or algorithms... The next step might be to group the data, based on the time of the session, ip and the user agent of each session as well as the fields web_links visited by the user of the session.

    I would be really grateful for all entries on where I'm wrong and all the solutions for it!

    Good. Please close the message.

    Concerning
    Girish Sharma

  • Using the Replace function

    Hi all

    I have a column called supplements and it has values such as

    1. the payment is > 90 days
    2. the payment is < = 10 days

    Now, I want to use the Replace function to replace > and < to "greaterthan".
    and 'lessthan' RESP, but the problem is that, in the function replace, I can give that one argument. So what I do is in the region of fx for the column

    Replace (supplements,' > ',' greaterthan')

    This replaces the > sign very well with string greaterthan and show me values such as

    1. the payment is greaterthan 90 days
    2. the payment is < = 10 days

    but I want to replace < "lessthen" also, how can I do? Can I undergo two replace statements? How can replace several string or special characters for a single column?

    Help, please.

    Thank you
    Ronny

    Try this
    CASE
    WHEN supplements LIKE ' % > %' THEN Replace(latepayment,'>','greaterthan')
    WHEN supplements LIKE ' %<=%' then=""><>
    END

  • Can you apply a 'or' within the Replace function

    I developed a script which will produce a flat file UTLFILE. It works for what I need, but my customer's requirement has changed when I need to apply a condition 'OR' with a function replacement:

    From:
    Replace (ALM_OG_PAYMENT_CODE, '6SMP', '51') to something like

    Replace (ALM_OG_PAYMENT_CODE, '6SMP' OR '5ach', 51) or maybe
    Replace (ALM_OG_PAYMENT_CODE, '%', 51) or something like that.

    Any ideas on that?

    -----------------------------------COMPLETE PROGRAM BELOW-------------------------------------------------------------------------------

    Set serveroutput ON SIZE 1000000
    set the position
    Set feedback off
    off Set trimspool
    trigger the echo
    set pagesize 0

    DECLARE
    v_data_file varchar2 (30);
    v_sch_code varchar2 (10);
    v_instance_name varchar2 (10);
    ws_path payroll.pybutfl.pybutfl_utl_file_path%TYPE;
    v_data_line VARCHAR2 (2000): = NULL;
    fhandle_o UTL_FILE.file_type;
    v_line_count NUMBER: = 0;
    v_selected_count NUMBER: = 0;
    v_error_count NUMBER: = 0;
    v_written_count NUMBER: = 0;
    v_error_text VARCHAR2 (50): = ' cannot write the line. ';
    v_errm VARCHAR2 (255);
    v_sqlerrm VARCHAR2 (255);
    v_payment_type VARCHAR2 (10);



    CURSOR c1 IS

    Select RTRIM
    ((to_char (ALM_OG_ACTIVITY_DATE, 'MMDDYYYY')) |'|) ' ||
    ALM_OG_NAME_PREFIX |' | ' ||
    ALM_OG_FIRST_NAME |' | ' ||
    ALM_OG_MI |' | ' ||
    ALM_OG_LAST_NAME |' | ' ||
    ALM_OG_NAME_SUFFIX |' | ' ||
    ALM_OG_DNR_SPOUSE_FIRST_NAME |' | ' ||
    ALM_OG_DNR_SPOUSE_MI |' | ' ||
    ALM_OG_DNR_SPOUSE_LAST_NAME |' | ' ||
    ALM_OG_DONOR_ADDRESS_1 |' | ' ||
    ALM_OG_DONOR_ADDRESS_2 |' | ' ||
    ALM_OG_DONOR_CITY |' | ' ||
    ALM_OG_DONOR_STATE_PROVINCE |' | ' ||
    ALM_OG_DONOR_ZIP |' | ' ||
    ALM_OG_PHONE_AREA | ALM_OG_PHONE_NUMBER |' | ' ||
    ALM_OG_PHONE_EXT |' | ' ||
    ALM_OG_EMAIL |' | ' ||
    ALM_OG_TOTAL_AMOUNT |' | ' ||
    ALM_OG_TOTAL_AMOUNT |' | ' ||
    Replace (ALM_OG_PAYMENT_CODE, '6SMP', '51') |' | ' ||
    Data_line ALM_OG_CAMPAIGN_CODE)

    of wsualumni.wsu_online_giving_table
    where ALM_OG_CAMPAIGN_CODE = 'WDET.

    /**********************************************************************/
    / * Program main treatment * /.
    /**********************************************************************/

    BEGIN



    Dbms_output.put_line ('program began in wsualumni.wsu_online_giving_online");
    IF (fhandle_o) UTL_FILE.is_open
    THEN
    UTL_FILE.fclose (fhandle_o);
    END IF;

    / * Name of the file here * /.
    v_data_file: = ("WDET_" |) To_char (sysdate, 'dd-MON-yyyy') | "_OG_DATA.txt");



    SELECT RTRIM (pybutfl_utl_file_path)
    IN ws_path
    OF payroll.pybutfl;



    fhandle_o: = UTL_FILE.fopen (ws_path, v_data_file, 'w');
    Dbms_output.put_line (' file UTLFILE for this race is: ' | ws_path |) » /'|| v_data_file);
    v_written_count: = 0;




    C1_rec in C1 LOOP
    BEGIN
    v_selected_count: = v_selected_count + 1;
    v_data_line: = rtrim (c1_rec.data_line);
    UTL_FILE.put_line (fhandle_o, v_data_line);
    v_written_count: = v_written_count + 1;
    EXCEPTION
    WHILE OTHERS
    THEN
    Dbms_output.put_line (v_error_text);
    v_error_count: = v_error_count + 1;
    END;
    END LOOP;

    Dbms_output.put_line (' number of records selected: ' | v_selected_count);
    Dbms_output.put_line (' number of records written: ' | v_written_count);

    IF (fhandle_o) UTL_FILE.is_open
    THEN
    UTL_FILE.fclose (fhandle_o);
    END IF;




    END;
    /

    Hello

    Regular expressions are much, much better for this kind of thing.

    REGEXP_REPLACE ( alm_og_payment_code
                   , '(6SMP|5ach)'
                   , '51'
                   )
    

    replaces all occurrences of '65MP' or '5ach' by ' 51 ".

    It works in Oracle 10 (and more).

  • Replace function does not work when I ran into 2 tables of reference

    I would really appreciate help with this one

    I want to update the records in Table1 using REPLACE the product, only if they meet certain conditionS in TABLE1 and TABLE2.
    The regular updating by using only the SET statement works fine, but when I use the statement REPLACE I get errors.

    This is my code:

    UPDATE TABLE1
    SET TEXT_VALUE = REPLACE (TEXT_VALUE, 'VALUE1', 'VALUE2')
    from TABLE1 O JOIN TABLE2 L ON O.OBJECT_ID = L.OBJECT_ID
    WHERE the O.TEXT_VALUE as "CONDITION1" AND L.CHECK_INOUT = "CONDITION2";

    I get the following error:

    Command line error: column 3: 1
    Error report-
    SQL error: ORA-00933: SQL not correctly completed command
    00933 00000 - "not correctly completed SQL command.
    * Cause:
    * Action:

    Thank you very much
    Elias

    UPDATE TABLE1: O
    SET TEXT_VALUE = REPLACE (TEXT_VALUE, 'VALUE1', 'VALUE2')
    WHERE O.OBJECT_ID IN (SELECT L.OBJECT_ID FROM TABLE2 L WHERE L.CHECK_INOUT = "CONDITION2")
    AND O.TEXT_VALUE like 'CONDITION1.
    /

    SY.

  • Application of condition in function


    Hello experts!

    I have a requirement where I want to execute 1 query based on the outcome of who I want to run the second...

    For example:

    I want to run a county of v$ ASM_DISK view and if it returns 0 more lines, then I want to run a second query... Is this possible? If so, please can someone send an example?

    BTW, I don't want to use the unit of PL/SQL or a procedure/function

    Thanks in advance

    If a condition is based result it can be easy to line:

    SQL > select "True" to double

    where the 2 (select count (*) from dba_views) > 0

    3;

    "TRU

    ----

    True

    SQL > select "True" to double

    2 where (select count (*) in dba_views)<>

    no selected line

    Kind regards.

  • '&amp;' in the Replace function

    Hi all


    I use the replcase function in generatig xml as below

    Select Replace ("Test & New", "&", "& amp ;')") Double;

    It asks me to enter the value (this is to be considered as subtitute value). How can I manipulate '&' in the function replcase to replace the respective value.

    Thanks in advance

    This can be useful
    Escape the sign & (and) in queries

  • Look of cells upward and replace function in numbers

    Hi here's a video explaining what Im trying to do. http://www.screencast.com/t/zrR5wDKEm4j7

    or here https://s3.amazonaws.com/edferrigancoaching/Video/Eds+Numbers+Request.mp4

    What I have is 2 tables. A collection of data. The other is summarizing the data. I want to finish with only 4-5 "buckets" in table two. I need a formula to take the content in a cell in the table 2 back to the one table and find all occurrences in this column and replace them with content I enter in a cell in front of the cell I want changed. The video will illustrate this.

    I searched on Fiverr experts of numbers, but only two people announced the knowledge of numbers and no one knew how to this number. Thank you this will help a lot on a long term project, I want to use it with over and over again. Ed

    < email published by host >

    Why not use the search and replace in the menu "Edit > find > find...» »

    This brings a dialog box where you can enter the search text and what to replace this text with:

  • Find and replace function in file names?

    Is it possible to do a find and replace in the names of files, rather than the office or similar?  I have a large list of names of files that all have a problem with them I want to replace, but I don't want to pass by and do each manually!  A better way to do it?

    Rob

    Hello exoticdisease,

    Thanks for posting on the Microsoft answers Forum.

    Select the files in Windows Explorer, press and hold Shift and right click on them. Choose copy from the menu in the path. Paste the list in any editor you prefer. You get the full path for each file, but a simple find/replace will clean up if necessary.

    I don't know if it of what you're looking for or not.

    If please reply and let us know if this solves your problem or if you need further assistance.

    Marilyn
    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think.

  • If the Replace function can be used

    Hi all

    I use 11 GR 2 and I love out to take the form of the below output in a row. rather than retrieve the output from "/ db/ptmtrain/data1/system01.dbf" is should be something like "/ db/rdstrain/data1/system01.dbf".

    SQL > select name from v$ datafile where rownum < 6;

    +++++++

    NAME

    -----------------------------------------------------------

    /DB/ptmtrain/Data1/System01.dbf

    /DB/ptmtrain/data2/sysaux01.dbf

    /DB/ptmtrain/Data1/undotbs01.dbf

    /DB/ptmtrain/Data3/tools01.dbf

    /DB/ptmtrain/data24/users01.dbf

    change as

    SQL > select name from v$ datafile where rownum < 6;

    NAME

    -----------------------------------------------------------

    /DB/rdstrain/Data1/System01.dbf

    /DB/rdstrain/data2/sysaux01.dbf

    /DB/rdstrain/Data1/undotbs01.dbf

    /DB/rdstrain/Data3/tools01.dbf

    /DB/rdstrain/data24/users01.dbf

    ++++++++

    Concerning

    Select Replace (name, 'ptmtrain', 'rdstrain') of v$ datafile where rownum<>

  • Replace function

    I have a string like below
    
    RPAD(INITCAP(acct_descr), 40, ' ')
    
    It should be changed as follows
    
    RPAD(INITCAP(acct_descr), 40, '' '')
    Thank you

    Hello

    What you trying to do?
    You want to replace each single quote in string str with two single quotes?
    If so,.

    REPLACE ( str
            , Q'{'}'
            , Q'{''}'
            )
    

    The above expression gives the same results as the one below, but can be less confusing because single quotes are literals:

    REPLACE ( str
            , ''''
            , ''''''
            )
    

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements), and the results you want from this data.
    In the case of a DML (UPDATE), for example, the sample data should show what looks like the tables before the DML, and the results will be the content of the or the tables changed after the DML.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

  • Color replace functionality 9 PRE - can't find it? Deleted pre 8?

    I found this information using Adobe first Elements 8:

    Replace color

    The colour effect replace replaces all occurrences of a selected by a new color, color preserving levels of gray. Use for this purpose, you can change the color of an object in an item by selecting it, and then adjusting the controls to create a different color.

    Replace a color

    1. Apply the effect.

    2. In the Control Panel tasks, select the effect, and click the button change the effects. Then click on the Setup button to the right of the effect name.

    3. Replace in the color dialog box, select the color you want to replace by clicking a color in the left box (the pointer becomes an eyedropper), or by clicking the target color swatch and selecting a color in the color picker dialog box.

    4. Choose the replacement color by clicking the color chart to replace.

    5. Expand or reduce the range of the color you replace by dragging the similarity slider.

    6. Select the colors United property to replace the specified color without preserving grayscale, and then click OK.

    7. Click done

    HOWEVER - in first Elements 9 - when you click the button change effects IS NOT 'KEY SETUP' to the right of the name of the effect...? There is therefore no dialog box replace color, etc... The only thing to the right of the effect name Image control is the icon of the small chronograph...

    Did I miss something or something changed in this version?

    Thanks for your help!

    Kim

    Just see this FAQ entryon pre 9 effects for the Mac. Never noticed before.

    Hunt

Maybe you are looking for