remove only one leading 0 (merged) [message #459046] |
Thu, 03 June 2010 03:33  |
buck444
Messages: 80 Registered: January 2006 Location: Luxembourg
|
Member |
|
|
Hi,
I receive source data with leading zeros. The Rules says: "If there is a leading 0 it has to be removed and data has to be shown starting with character 2".
Here is my code I started with:
CREATE TABLE leading_0_test
(
col_1 VARCHAR2(10)
)
insert into leading_0_test values('00123')
select trim(leading'0' from COL_1),trim(leading'0' from substr(COL_1,1,1)),COL_1 from leading_0_test
The first trim removes all leading zeros. Therefore I tried to solve it with the substr, but without success.
Any ideas?
thanks in advance.
|
|
|
Re: remove only one leading 0 (merged) [message #459049 is a reply to message #459046] |
Thu, 03 June 2010 04:01   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Why do you want to use SUBSTR when TRIM or LTRIM do it good?
I don't understand what you mean with
Quote:If there is a leading 0 it has to be removed and data has to be shown starting with character 2
Does it mean that if you have leading 0 then you put a 2 before the actual (number) value?
If you post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.
Regards
Michel
[Updated on: Thu, 03 June 2010 04:02] Report message to a moderator
|
|
|
|
Re: remove only one leading 0 (merged) [message #459056 is a reply to message #459049] |
Thu, 03 June 2010 04:17   |
Its_me_ved
Messages: 979 Registered: October 2009 Location: India
|
Senior Member |
|
|
Quote:
The Rules says: "If there is a leading 0 it has to be removed and data has to be shown starting with character 2".
I think he wants to remove 1st leading 0 and he wants to display starting from second position?
SQL> ed
Wrote file afiedt.buf
1 with t as
2 (select '00123' a from dual union all select '12345' from dual)
3 select case when substr(a,1,1)='0' then substr(a,2,length(a))
4* else a end output from t
SQL> /
OUTPU
-----
0123
12345
Regards,
Ved
|
|
|
Re: remove only one leading 0 (merged) [message #459061 is a reply to message #459049] |
Thu, 03 June 2010 04:28   |
buck444
Messages: 80 Registered: January 2006 Location: Luxembourg
|
Member |
|
|
Hi,
"Its_me_ved": Thanks for your help. This is a option that will do it for me.
michel: yes I was looking for a solution like mentioned by "Its_me_ved".
If the first character is a 0, then I need to remove 1st leading 0 and want to display starting from second position.
|
|
|
|
|
Re: remove only one leading 0 (merged) [message #459068 is a reply to message #459067] |
Thu, 03 June 2010 04:40  |
 |
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
Why not use regular expressions?
column new_col1 format a10
With yourtable As
( Select cast('00123' as varchar2(10)) col1 from dual union all
Select cast('9876' as varchar2(10)) col1 from dual union all
Select cast('0789' as varchar2(10)) col1 from dual union all
Select cast('AB00A' as varchar2(10)) col1 from dual union all
Select cast('00012' as varchar2(10)) col1 from dual
)
select col1
, regexp_replace(col1, '^0','') new_col1
from yourtable
/
MHE
[Updated on: Thu, 03 June 2010 04:40] Report message to a moderator
|
|
|