前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Oracle nvarchar2 错误解决方案

Oracle nvarchar2 错误解决方案

作者头像
星哥玩云
发布2022-08-18 14:24:18
4060
发布2022-08-18 14:24:18
举报
文章被收录于专栏:开源部署

问题:

使用  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.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档