文字列を数値に変換する
「cast( <カラム> as <数値型> )」でテーブル内の文字列型のカラムを数値に変換して取り出すことができます。
↓のようなテーブルがあるとして、
$ db2 "describe table test" Column Type Type name schema name Length Scale Nulls ------------------------------ --------- ------------------ -------- ----- ------ STR SYSIBM VARCHAR 20 0 No 1 record(s) selected.
$ db2 "select * from test" STR -------------------- 1 2 02 x 5 record(s) selected.
テーブルのstr列をBIGINTに変換して取り出してみます。
$ db2 "select str, cast( str as BIGINT ) from test" STR 2 -------------------- -------------------- 1 1 2 2 02 2 SQL0420N Invalid character found in a character string argument of the function "BIGINT". SQLSTATE=22018
変換できな場合はエラーになるのか。ふむ。
参考: