I would like to extract the first 6 digits of a numeric value (e.g.
the string '123456' out of the numeric 123456789012345 6789). I tried
a combination of CAST and SUBSTR, but it seems I am not doing the
conversion properly. Please advice:
$ cat substr.sql
connect to viper
@
create table largenum (id integer not null, lval decimal(21,0) not
null)
@
insert into largenum values (1, 123456789012345 6789)
@
select lval from largenum
@
select substr(cast(lva l as char), 1, 6) from largenum
@
drop table largenum
@
connect reset
@
terminate
@
$ db2 -td@ -f substr.sql
Database Connection Information
Database server = DB2/LINUX 9.5.0
SQL authorization ID = DB2INST3
Local database alias = VIPER
DB20000I The SQL command completed successfully.
DB20000I The SQL command completed successfully.
LVAL
-----------------------
123456789012345 6789.
1 record(s) selected.
SQL0138N A numeric argument of a built-in string function is out of
range.
SQLSTATE=22011
DB20000I The SQL command completed successfully.
DB20000I The SQL command completed successfully.
DB20000I The TERMINATE command completed successfully.
--
the string '123456' out of the numeric 123456789012345 6789). I tried
a combination of CAST and SUBSTR, but it seems I am not doing the
conversion properly. Please advice:
$ cat substr.sql
connect to viper
@
create table largenum (id integer not null, lval decimal(21,0) not
null)
@
insert into largenum values (1, 123456789012345 6789)
@
select lval from largenum
@
select substr(cast(lva l as char), 1, 6) from largenum
@
drop table largenum
@
connect reset
@
terminate
@
$ db2 -td@ -f substr.sql
Database Connection Information
Database server = DB2/LINUX 9.5.0
SQL authorization ID = DB2INST3
Local database alias = VIPER
DB20000I The SQL command completed successfully.
DB20000I The SQL command completed successfully.
LVAL
-----------------------
123456789012345 6789.
1 record(s) selected.
SQL0138N A numeric argument of a built-in string function is out of
range.
SQLSTATE=22011
DB20000I The SQL command completed successfully.
DB20000I The SQL command completed successfully.
DB20000I The TERMINATE command completed successfully.
--
Comment