网站建设所用程序,flash网站什么意思,计算机毕设做网站难吗,制作网站代码吗文章目录 判断字符串是否全为数字1、使用 trimtranslate 函数2、使用 replacetranslate 函数3、使用 regexp_like 函数 translate 函数讲解1、基本用法#xff0c;字符对应替换2、 如果 to_string 没有对应字符则删除额外的字符3、如果 to_string 对应字符过多不影响4、如果 t… 文章目录 判断字符串是否全为数字1、使用 trimtranslate 函数2、使用 replacetranslate 函数3、使用 regexp_like 函数 translate 函数讲解1、基本用法字符对应替换2、 如果 to_string 没有对应字符则删除额外的字符3、如果 to_string 对应字符过多不影响4、如果 to_string 为空字符则直接返回 NULL5、如果想删除 expr 中某些字符除了 from_strimg 拥有这些字符外还应传入一个不相关字符同时 to_string 中也需要传入该字符6、 如果 from_strimg 相同字符对应 to_string 多个字符按去第一个8、判断 from_strimg 中包含的字符是否一致排序可以不同9、同 replace 函数比较 判断字符串是否全为数字
最近在工作中遇到这样一个问题判断字符串中是否全为数字Oracle 数据库的操作方法有以下几种
1、使用 trimtranslate 函数
select * from dual where trim(translate(column,0123456789, )) is NULL;这里要注意的是translate 函数的第三个参数是一个空格不是’’, 因为translate的第三个参数如果为空的话那么永远返回’’,这样的就不能达到过滤纯数字的目的。这样把所有的数字都转化为空格如果全部是由数 构成那么一旦trim后自然是空实现了上述目标。当然如果想排除空项的话可以这样写
select * from dual where trim(translate(nvl(column,x),0123456789, )) is NULL;--x 表示任何0-9以外的字符。NVL函数是一个空值转换函数 NVL(exp1exp2)函数如果exp1为空值则返回exp2否则返回exp1。 注意当 column 的值为空格时也会被筛选到所以比较好的方法是使用 replacetranslate 函数。
2、使用 replacetranslate 函数
select * from dual where replace(translate(column,0123456789,0),0,) is NULL;3、使用 regexp_like 函数
select * from dual where regexp_like(column,^[0-9][0-9]$)这里需要注意的是regexp_like 函数不是在所有的 Oracle 版本中都能使用。regexp_like 是 Oracle 支持正则表达式的函数中的一个共有四个函数regexp_like regexp_replaceregexp_instrregexp_substr。
translate 函数讲解
上述方法比较关键的是 translate 函数对于该函数查看相关讲解如下
translate(expr, from_strimg, to_string)translate 函数返回 expr其中 from_string 中的每个字符都被 to_string 中的相应字符替换。若 expr 中某个字符未出现在 from_string 中则该字符不会被替换。如果 expr 是一个字符串那么你必须把它放在单引号中。 from_string 参数值可以包含比 to_string 更多的字符。在这种情况下from_string 末尾的多余字符在 to_string 中没有对应的字符。如果这些额外的字符出现在 expr 中那么它们将从 expr 返回值中移除。
to_string 参数值为空字符串时expr 返回值中删除 from_string 中的所有字符。Oracle 数据库将空字符串解释为空如果此函数具有空参数则返回 null。
translate 提供了与 replace 函数相关的功能。 replace 允许用一个字符串替换另一个字符串以及删除字符串。 translate 允许在一个操作中进行多个单字符一对一的替换。
实际案例
1、基本用法字符对应替换
select translate(1234567,123 ,abc) from dual ;--1替换为a,2替换为b,3替换为c
-------
abc45672、 如果 to_string 没有对应字符则删除额外的字符
select translate(1234567,123 ,ab) from dual--‘1234567’中的‘3’被删掉;
-----
ab45673、如果 to_string 对应字符过多不影响
select translate(1234567,123 ,abccd) from dual ---‘123’对应‘abc’4、如果 to_string 为空字符则直接返回 NULL
select translate(1234567,123 ,) from dual
----
null
5、如果想删除 expr 中某些字符除了 from_strimg 拥有这些字符外还应传入一个不相关字符同时 to_string 中也需要传入该字符
SELECT translate(0123456789,123 ,) FROM dual
----
0456789
其实原理和 3 一样’123’比’长额外的’123’字符在 expr 中出现因此去除掉 expr 中的’123‘。
需要注意的是如果更改’字符在 from_strimg 中的位置结果就不一样了。
SELECT translate(0123456789,123 ,) FROM dual
----
0456789
原因在于’123‘中的’1’字符和’对应则’23就属于额外的字符从 expr 返回值中删除’23’字符。
6、 如果 from_strimg 相同字符对应 to_string 多个字符按去第一个
SELECT translate(01233456789,1233 ,abcd) FROM dual
----
0abcc456789
7、 如果想保留某些特定字符筛选掉其他的
比如筛掉汉字保留数字
先删除数字
SELECT translate(我5喜2欢1你,0123456789 ,) FROM dual
-----
我喜欢你
再用筛选出的汉字去筛选原来的语句留下数字
SELECT translate(我5喜2欢1你,||translate(我5喜2欢1你,0123456789 ,) ,) FROM dual
----
521
8、判断 from_strimg 中包含的字符是否一致排序可以不同
SELECT 1 FROM dual where translate(abcdefgh,bcd ,111111)translate(abcdefgh,cbd ,111111)
----
1
----反之
SELECT 1 FROM dual where translate(abcdefgh,bdd ,111111)translate(abcdefgh,cbd ,111111)
----
null
9、同 replace 函数比较
select translate(itmyhome#163%com, #%, .) from dual;
select replace(itmyhome#163%com, #%, .) from dual;---------
itmyhome163.com
itmyhome#163%com 文章转载自: http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.rcqyk.cn.gov.cn.rcqyk.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.blbys.cn.gov.cn.blbys.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.mlfmj.cn.gov.cn.mlfmj.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.junmap.com.gov.cn.junmap.com