设为首页 加入收藏

TOP

Oracle nvarchar2 错误解决方案
2019-07-10 20:11:26 】 浏览:78
Tags: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 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.


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MongoDB的集群模式 - Replica Set 下一篇Oracle 18.3 ORA-12012 ORA-20001

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目