【DB2基础】DB2中字符、数字和日期类型之间的转换
转自:http://space.itpub.net/785478/viewspace-571157
一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类型之间做转换,那么在DB2和Oracle中,他们分别是如何实现的呢?在Oracle这几个类型之间的转换是十分方便的,通过to_char|to_date|to_number函数即可完成类型转换。本小节主要介绍DB2中的一些知识,从Oracle转过来的DBA或开发人员,可以对比着学习。
DB2中的col_a字段 | |
| 字段类型 | 到字符类型的转换 |
| smallint | char(col_a) |
| integer | |
| bigint | |
| double | char(cast(col_a as decimal(8,2)))) |
| decimal(8,2) | Digits(col_a) |
测试一下:
SQL>create table test_datatype
SQL>(
SQL>col_a smallint,
SQL>col_b integer,
SQL>col_c bigint,
SQL>col_d decimal(8,2),
SQL>col_e double
SQL>);
DB20000I SQL命令成功完成。 SQL>insert into test_datatype values(1,20,3000,4000.44,55555);
DB20000I SQL命令成功完成。 SQL>describe select * from test_datatype
列信息 列数:5 SQL 类型 类型长度 列名 名称长度 -------------------- ----------- ------------------------------ ----------- 501 SMALLINT 2 COL_A 5 497 INTEGER 4 COL_B 5 493 BIGINT 8 COL_C 5 485 DECIMAL 8, 2 COL_D 5 481 DOUBLE 8 COL_E 5 SQL>select * from test_datatype;
COL_A COL_B COL_C COL_D COL_E ------ ----------- -------------------- ---------- ------------------------ 1 20 3000 4000.44 +5.55550000000000E+004 1 条记录已选择。 SQL>select char(col_a),char(col_b),char(col_c),digits(col_d),char(cast(col_e as decimal(8,2))) from test_datatype;
1 2 3 4 5 ------ ----------- -------------------- -------- ---------- 1 20 3000 00400044 055555.00 1 条记录已选择。 SQL>select char(col_a)||','||char(col_b)||','||char(col_c)||','||digits(col_d)||','||char(cast(col_e as decimal(8,2))) from test_datatype;
1 ----------------------------------------------------------- 1 ,20 ,3000 ,00400044,055555.00 1 条记录已选择。 SQL>
DB2中的col_a字段 | ||
| 字段类型 | 数字类型 | 转换 |
| char | smallint | Integer(trim(char(col_a))) |
| integer | ||
| bigint | cast(char(col_a) as bigint) | |
| decimal | cast(col_a as decimal(8,2)) | |
| double | cast(cast(char(cast(a.col_a as decimal(8,2))) as decimal(8,2)) as double) | |
测试一下
SQL>create table test_datatype
SQL>(
SQL>col_a char(2),
SQL>col_b char(4),
SQL>col_c char(10),
SQL>col_d char(10),
SQL>col_e char(10)
SQL>);
DB20000I SQL命令成功完成。 SQL>insert into test_datatype values('1','200','30000','4000.04','5000000');
DB20000I SQL命令成功完成。 SQL>describe select * from test_datatype;
列信息 列数:5 SQL 类型 类型长度 列名 名称长度 -------------------- ----------- ------------------------------ ----------- 453 CHARACTER 2 COL_A 5 453 CHARACTER 4 COL_B 5 453 CHARACTER 10 COL_C 5 453 CHARACTER 10 COL_D 5 453 CHARACTER 10 COL_E 5 SQL>select Integer(trim(char(col_a))),
SQL>Integer(trim(char(col_b))),
SQL>cast(char(col_c) as bigint),
SQL>cast(col_d as decimal(8,2)),
SQL>cast(cast(char(cast(col_e as decimal(12,2))) as decimal(12,2)) as double)
SQL>from test_datatype;
1 2 3 4 5 ----------- ----------- -------------------- ---------- ------------------------ 1 200 30000 4000.04 +5.00000000000000E+006 1 条记录已选择。 SQL>
DB2中的col_a字段 | |
| 字段类型 | 日期时间型 |
char | date(trim(char(col_a))) timestamp(trim(char(col_a)))或to_date(--string-expression,format-string(DB2 9) |
| 注:to_date实际是TIMESTAMP_FORMAT函数的一个同义词 | |
| 字段类型 | 字符类 |
| date | char(col_a) |
| time | |
| timestamp | char(col_a) 或 to_char(--timestamp-expression--,format-string--) (DB2 9) |
| 注:to_char实际是VARCHAR_FORMAT函数的一个同义词 | |
测试一下看看
SQL>select date(trim(char('2009-09-01 '))),
SQL>time(trim(char('12:23:34 '))),
SQL>timestamp(trim(char('2009-02-26-14.28.40.234000'))) from sysibm.dual;
1 2 3 ---------- -------- -------------------------- 2009-09-01 12:23:34 2009-02-26-14.28.40.234000 1 条记录已选择。 SQL>select char(current date),char(current time),char(current timestamp) from sysibm.dual;
1 2 3 ---------- -------- -------------------------- 2009-02-26 14:28:40 2009-02-26-14.28.40.234000 1 条记录已选择。 SQL>select to_char(current timestamp,'yyyy-mm-dd hh24:mi:ss') from sysibm.dual;
--DB2 9版本中新增 1 -------------------------- 2009-02-26 14:45:53 1 条记录已选择。 SQL>select to_date('2009-01-01 12:23:45','yyyy-mm-dd hh24:mi:ss') from sysibm.dual;
--DB2 9版本中新增 1 -------------------------- 2009-01-01-12.23.45.000000 1 条记录已选择。 SQL>
posted on 2012-09-12 21:37 LeeXiaoLiang 阅读(955) 评论(0) 收藏 举报
浙公网安备 33010602011771号