SQL判断字符串⾥不包含字母Oracle:
⽅法⼀:通过To_Number 函数异常来判断,因为这个函数在转换不成功的时候是报错,所以只能⽤存储过程包装起来.
CREATE OR REPLACE FUNCTION Is_Number (
str_    VARCHAR2 ) RETURN VARCHAR2
IS
num_    NUMBER;
BEGIN
num_ := to_number(str_);
RETURN 'Y';
EXCEPTION
WHEN OTHERS THEN
RETURN 'N';
END Is_Number;
/
测试:
SQL> select Is_Number('12345') from dual;
IS_NUMBER('12345')
-------------------------
Y
SQL> select Is_Number('123.45') from dual;
IS_NUMBER('123.45')
--------------------------
Y
SQL> select Is_Number('123a') from dual;
IS_NUMBER('123A')
-----------------------
N
⽅法⼆:通过REGEXP_REPLACE/REGEXP_LIKE函数来实现
SQL> select nvl2(TRIM(REGEXP_replace('ss88','[0-9|+|-|.]','')),0,1) is_numerical from dual;
IS_NUMERICAL
------------
SQL> select nvl2(TRIM(REGEXP_REPLACE('2.88','[0-9|+|-|.]','')),0,1) is_numerical from dual;
IS_NUMERICAL
-
-----------
1
SQL> SELECT CASE WHEN REGEXP_LIKE('1254','^[ +-.0-9]{4}$') then 1 else 0 end as is_numerical from dual;
IS_NUMERICAL
------------
1
⽅法三:通过 translate函数来实现,这个函数很强⼤,这⾥顺便提下它的语法和其他⽤处.
TRANSLATE ( 'char' , 'from_string' , 'to_string' )
TRANSLATE returns char with all occurrences of each character in from_string replaced by its corresponding character in to_string.
Characters in char that are not in from_string are not replaced. The argument from_string can contai
n more characters than to_string.
In this case, the extra characters at the end of from_string have no corresponding characters in to_string. If these extra characters appear in char,then they are removed from the return value.You cannot use an empty string for to_string to remove all characters in from_string from the return value. Oracle interprets the empty string as null, and if this function has a null argument, then it returns null.
SELECT nvl2(translate('123','/1234567890','/'),'CHAR','NUMBER')
FROM  dual ;
SQL> SELECT nvl2(translate('123','/1234567890','/'),'CHAR','NUMBER')  TYPE
2  FROM  dual;
TYPE
------------------------------
NUMBER
SQL>
SQL> SELECT nvl2(translate('123A','/1234567890','/'),'CHAR','NUMBER') TYPE
2  FROM  dual;
TYPE
------------------------------
CHAR
在实际使⽤过程中,可以根据情况选⽤这两种⽅法。第⼀种⽅法的好处是可以判断带⼩数的数字,⽽第⼆种⽅法只能判断整数。
因为如果 translate 函数如果写成 translate('123A','/1234567890.','/') 这种形式是会有问题的。
SQL> select Is_Number('123.45.6') TYPE from dual;
TYPE
--------------------------------------------------------------------------------
N
SQL> SELECT nvl2(translate('1234.5.6','/1234567890.','/'),'CHAR','NUMBER')  TYPE FROM  dual;
TYPE
------------------------------
NUMBER
解释:
Translate中,每个from_string中的字符被to_string中相应的字符所代替。
select translate('acdd','cd','ef') from dual;→aeff
如果from_string⽐to_string长,那么from_string中多余的字符将被移除。
select translate('acdd','acd','ef') from dual;→ef (a由e 代替,c由f代替,d就被移除)
select translate('acdd','cda','ef') from dual;→eff(c由e 代替,d由f代替,a就被移除)
如果to_string为空,或者两者都为空,那么返回char也为空。所以to_string不能为空。
select translate('acdd','cd','') from dual;→ (空)
select translate('acdd','','') from dual;→(空)
实战:
如何判断⼀个字符串是否是数字?
解:先转换:由于to_string不能为空,我们巧⽤#号代替
select translate('abc123','#1234567890.','#') from dual;→abc
from_string 中的#被to_string中的#代替,但char中⼜没有#字符,所以通过这⼀步躲开了to_string必须不为空的规则。
然后后⾯的数字以及⼩数点都转换为空,于是原来的字符串中只留下abc三个字符。
转换好后,⽤ nvl2判断即可:
select nvl2(translate('abc123','#1234567890.','#'),'字符串','数字') from dual;→字符串
nvl2的作⽤就是,NVL2 (expr1, expr2, expr3) ->expr1不为NULL,返回expr2;为NULL,返回expr3.
从⼀串⽂本中提取数字:
select translate('⽤纸箱包200/箱','#'||REGEXP_replace('⽤纸箱包200PCS/箱','[0-9]',''),'#')from dual;
利⽤TRANSLATE实现关键字的过滤
有时候需要对⼀些关键词语进⾏过滤,直接使⽤replace的话,可能由于这些关键词语⽐较多⽽要嵌套使⽤,语句也不好写,同时也浪费资源。
regexp like这种情况其实可以使⽤TRANSLATE和replace组合使⽤就能完全达到⽬的了。⽐如要将“深圳”、“北京”等作为关键词语,在显⽰内容是要将这些词语过滤掉不显⽰:
Sql代码
1. --⾸先使⽤TRANSLATE将关键词语统⼀转换成⼀个特殊的字符串,⽐如这⾥的X
2.
3. SQL> select TRANSLATE('上海北京天津重庆⼴州深圳武汉','深圳北京','XXXX') from dual;
4. TRANSLATE('上海北京天津重庆⼴?
5. ------------------------------
6. 上海XX天津重庆⼴州XX武汉
7. --然后⽤replace将特殊的字符串替换掉。注意:不能⽤TRANSLATE直接将关键词语直接转换为''字符串
8.
9. SQL> select replace(TRANSLATE('上海北京天津重庆⼴州深圳武汉','深圳北京','XXXX'),'X') from dual;
10. REPLACE(TRANSLATE('上海北京天?
11. ------------------------------
12. 上海天津重庆⼴州武汉
13.
14. SQL> --但是,⽤TRANSLATE是以⼀个字符为单位的,只要匹配到都会转换。⽐如不管“北”和“京”是否连接在⼀起都会做转换
15. SQL> select TRANSLATE('上海京天津重庆北⼴州深圳武汉','深圳北京','XXXX') from dual;
16. TRANSLATE('上海京天津重庆北⼴?
17. ------------------------------
18. 上海X天津重庆X⼴州XX武汉
TRANSLATE(string,from,to)转换的两个注意
1、转换源字串(from)在⽬的字串(to)中不存在对应,则转换后被截除
2、转换⽬的字串(to)不能为'',''在oracle中被视为空值,因此⽆法匹配⽽返回为null
另外,⼀个汉字作为⼀个字符还是两个字符进⾏转换与字符集的设置相关。
replace:字符串级别的代替
如:SELECT REPLACE('acdd','cd','ef') FROM dual; →aefd
translate:字符级别的代替
如:SELECT TRANSLATE('acdd','cd','ef') FROM dual; →aeff
REPLACE ( char , search_string [, replacement_string] )
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
If replacement_string is omitted or null, then all occurrences of search_string are removed.
If search_string is null, then char is returned.
解释:repalce中,每个search_string都被replacement_string所代替。
select replace('acdd','cd','ef') from dual;→ aefd
如果replacement_string为空或为NULL,那么所有的search_string都被移除。
select replace('acdd','cd','') from dual;→ad
如果search_string为null,那么就返回原来的char。
select replace('acdd','','ef') from dual;→acdd
select replace('acdd','','') from dual; →acdd(也是两者都为空的情况)
Both search_string and replacement_string, as well as char, can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char.
This function provides functionality related to that provided by the TRANSLATE function.
TRANSLATE provides single-character, one-to-one substitution.
REPLACE lets you substitute one string for another as well as to remove character strings.
SQLSever:
ISNUMERIC
ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0. Valid numeric data types include the following: int numeric
bigint money
smallint smallmoney
tinyint float
decimal real
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols, see .
SELECT ISNUMERIC('$') as is_numerical
is_numerical
--------------
1
SELECT ISNUMERIC('1.0E5') as is_numerical
is_numerical
-------
1
ISNUMERIC 校验时认为科学计数法、货币符号均为合法,如果此类数据不加转换
直接插⼊数据库的float、decimal等字段中是会出异常。以下函数可⽤于校验字符串是否为数字类型,仅允许输⼊+、-、0-9、.字符.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。