> I would like to extract the first 6 digits of a numeric value (e.g.
> the string '123456' out of the numeric 1234567890123456789). I tried
[quoted text clipped - 56 lines]
>
> --
> db2 "select substr(cast(lval as char(22)),3,6) from largenum"
Thank you for your replay Lennart. I have some follow-up questions:
1. Why are leading zeros added in the cast from numeric to char?
2. How do I reliably remove them?
3. Consider the below sample: What query returns the string '123456'
for the all rows?
create table largenum (lval decimal(21,0) not null)
@
insert into largenum values
(1234567890123456789),
( 123456789012345678),
( 12345678901234567),
( 1234567890123456),
( 123456789012345),
( 12345678901234),
( 1234567890123),
( 123456789012)
@
select substr(cast(lval as char(64)), 3, 6) as as_char from largenum
@
AS_CHAR
-------
123456
012345
001234
000123
000012
000001
000000
000000
8 record(s) selected.
--
fynn00@googlemail.com - 11 Apr 2008 11:42 GMT
your previous code indicates you are on Version 9
> Database server = DB2/LINUX 9.5.0
> SQL authorization ID = DB2INST3
> Local database alias = VIPER
Did you try the STRIP function already?
db2 "values(STRIP('00123',L,'0'))"
1
-----
123
Regards,
Florian
Lennart - 11 Apr 2008 19:37 GMT
> > db2 "select substr(cast(lval as char(22)),3,6) from largenum"
>
[quoted text clipped - 36 lines]
>
> --
Something like:
select substr(cast(lval as char(22)), 22 - length(strip(cast(lval as
char(22)), L, '0')) + 1, 6) as as_char from largenum"
AS_CHAR
-------
123456
123456
123456
123456
123456
123456
123456
123456
/Lennart