remove extra + from text SQL

跟風遠走 提交于 2019-12-11 15:57:42

问题


this refers to a question asked by someone else previously previous question

my question is how do I adapt this solution so that before any function/script is ran the name and value fields are stripped of any additional + and updated so no additional + remain.

For e.g.

Name     Value
A+B+C+   1+2+3+
A++B     1++2

this should be updated to

   Name     Value
    A+B+C   1+2+3
    A+B     1+2

once this update has taken place, I can run the solution provided in the previous question.

Thanks


回答1:


You need to replace ++ with + and to remove the + at the end of the string.

/* sample data */
with input(Name, Value) as (
                            select 'A+B+C+'   ,'1+2+3+' from dual union all
                            select 'A++B'     ,'1++2'   from dual
                            )
/* query */
select trim('+' from regexp_replace(name,  '\+{2,}', '+') ) as name,
       trim('+' from regexp_replace(value, '\+{2,}', '+') ) as value
from input     

If you need to update a table, you may need:

update yourTable
set name = trim('+' from regexp_replace(name, '\+{2,}', '+') ),
    value= trim('+' from regexp_replace(value, '\+{2,}', '+') )

In a more compact way, without the external trim ( assuming you have no leading +):

/* sample data */
with input(Name, Value) as (
                            select 'A+B+C+'      ,'1+2+3+' from dual union all
                            select 'A++B+++C+'   ,'1++2+++3+' from dual union all
                            select 'A+B'         ,'1+2'   from dual
                            )
/* query */
select regexp_replace(name,  '(\+)+(\+|$)', '\2') as name,
       regexp_replace(value, '(\+)+(\+|$)', '\2') as value
from input    



回答2:


You could use something on the lines of:

 Select substr('1+2+3+', 0, length('1+2+3+')-1) from dual ;
 Select replace('1++2', '++', '+') from dual;

I'm assuming you have the output already present in a variable you can play with.

EDIT: Here's a function that can solve the problem (You can call this function in your select clauses thereby solving the problem):

CREATE OR REPLACE Function ReplaceChars
    ( name_in IN varchar2 )
    RETURN varchar2
IS
    changed_string varchar2(20) ;
BEGIN

    changed_string:=replace(name_in, '++', '+') ;

    CASE WHEN substr(changed_string, -1) in ('+')
        then
            changed_string:=substr(changed_string,0, length(changed_string) - 1) ;
        else changed_string:=changed_string ;
    end CASE ;

    RETURN changed_string;
END;



回答3:


You can use the below:

LTRIM(RTRIM (REGEXP_REPLACE (column_name, '\+{2,}', '+'), '+'),'+')

Eg:

    SELECT LTRIM(RTRIM (REGEXP_REPLACE ('+A+++B+C+++D++', '\+{2,}', '+'), '+'),'+') VALUE
  FROM DUAL;

returns output: A+B+C+D




回答4:


if youre working with ssms, GIVE IT A GO:::

UPDATE tablename
SET colname= 
CASE colname WHEN LIKE '%++%' THEN
    WHILE colname LIKE '%++%'
        (REPLACE(colname,++,+))
    END LOOP

WHEN LIKE '%+' THEN
    SUBSTR(colname, 1, LENGTH(colname)-1)

WHEN LIKE '+%' THEN
    SUBSTR(colname, 2, LENGTH(colname))

ELSE
   colname
END


来源:https://stackoverflow.com/questions/44365517/remove-extra-from-text-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!