20190707-Oracle nvarchar2错误

在这里插入图片描述

问题:

使用substr函数截取指定字符串时,取出4位字符 年(例如2019),结果只取出3位(例如201);

问题原因:

字符类型 nvarchar2

解决方案:

通过TRANSLATE函数将 nvarchar2 转换成 varchar2 。

问题重现:

1 创建测试数据

create table t1(id number,t_format nvarchar2(100),t_name varchar2(100));

insert into t1 values(1,'abcde2019xxx0707uuu','abcde2019xxx0707uuu');

commit;

2 通过substr函数截取年月日

select id,
       t_format,
       t_name,
       length(t_format),
       length(t_name),
       substr(t_format, 6, 4),
       substr(t_format, 13, 4),
       substr(t_name, 6, 4),
       substr(t_name, 13, 4)
  from t1;

在这里插入图片描述

select id,
     t_format,
     t_name,
     substr(t_format, 6, 5),
     substr(t_format, 13, 5),
     substr(t_name, 6, 4),
     substr(t_name, 13, 4)
from t1;

在这里插入图片描述

3 通过TRANSLATE函数将 nvarchar2 转换成 varchar2

select id,
       t_format,
       t_name,
    substr(utl_raw.cast_to_varchar2(utl_raw.cast_to_raw(Translate(t_format USING CHAR_CS))),6,4),
    substr(utl_raw.cast_to_varchar2(utl_raw.cast_to_raw(Translate(t_format USING CHAR_CS))),13,4),
       substr(t_name, 6, 4),
       substr(t_name, 13, 4)
  from t1;

在这里插入图片描述

https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements001.htm#SQLRF30020
NVARCHAR2 Data Type
The  NVARCHAR2  data type is a Unicode-only data type. When you create a table with an  NVARCHAR2  column, you supply the maximum number of characters it can hold. Oracle subsequently stores each value in the column exactly as you specify it, provided the value does not exceed the maximum length of the column.
The maximum length of the column is determined by the national character set definition. Width specifications of character data type  NVARCHAR2  refer to the number of characters. The maximum column size allowed is 4000 bytes.

欢迎关注我的微信公众号"IT小Chen"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值