Link to home
Start Free TrialLog in
Avatar of Sam OZ
Sam OZFlag for Australia

asked on

Get oracle view with end of value trimmed

I have an oracle 11g table TB_Doc with column DocNo among other columns
Sample values for DocNo
   Doc1
   Doc1_AR1
   Doc2
    Doc2_AR2
   Doc3
   Doc4
   Doc4_AR1

The ending _ARx need to be trimmed and I need to get a view with DerivedDoc as

  Doc1
   Doc1
   Doc2
    Doc2
   Doc3
   Doc4
   Doc4

   Please give me the Sql for the view
ASKER CERTIFIED SOLUTION
Avatar of Alex [***Alex140181***]
Alex [***Alex140181***]
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And without regular expressions:

create or replace view DerivedDoc as
select
  case 
    when DocNo like '%\_AR%' escape '\' then 
      substr(DocNo, 1, instr(DocNo, '_AR') - 1) 
    else 
      DocNo
    end as DerivedDocNo
from 
  tb_doc

Open in new window


Do be aware that this assumes that the data is as you show in your example and that you don't mind anything being removed after the '_ARx'. You could substitute Alexander's regex into this as he did not originally include the create view statement, but did really answer main part of the question.

Edit: Escaped the _ character as suggested by Alexander below.
@Martyn: this will erase everything after "_AR" even if it is found in the middle of the string. I doubt this is helpful... He's on 11gR2, so he should use regular expressions for stuff like that!
@Alexander: I know, and my example specifies this in the description. The OP does not specify that they do not want this to occur and imply that the _ARx will be at the end in their data. With that in mind, it may well be helpful. I have also commented that the OP could use your regex example (due credit given) in the create or replace view statement, which was part of the original request.
Apart from that, your query is wrong: you have to escape the '_'. If you do not, this will count as a wildcard character (1 char)!
You may test your proposed solution with the following string:
Doc1____AR_____AR1
and you'll notice it will fail
Thank you for that suggestion. I have amended my answer accordingly.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

@Sam,
Aside form the actual characters, how is this different from your previous question?

https://www.experts-exchange.com/questions/29121928/Oracle-Query-to-get-the-string-replaced-looking-a-pattern.html
Aside form the actual characters, how is this different from your previous question?
I thought this to myself ;-) It's virtually the same...