当前位置:网站首页>SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
2022-07-26 17:15:00 【晴朗H】
SQL判断某列中是否包含中文字符、英文字符、纯数字
一、包含中文字符
select * from 表名 where 列名 like '%[吖-座]%'
[吖-座]是中文字符集第一个到最后一个的范围,虽然匹配准确,但是用Like的效率很低
在使用mysql时候,某些字段会存储中文字符,或是包含中文字符的串,查询出来的方法是:
SELECT column FROM table WHERE length(column)!=char_length(column)
原理其实很简单,当字符集为UTF-8,并且字符为中文时,length() 和 char_length() 两个方法返回的结果是不相同的。
二、包含英文字符
select * from 表名 where 列名 like '%[a-z]%' or like '%[A-Z]%'
三、包含纯数字
select * from 表名 where 列名 like '%[0-9]%'
方法二:通配符。高逼格使用。
适用场景:2005及以上(2005之前的版本没试过,应该也支持)
--返回0-则为纯数字(支持正负数,小数点)
SELECT PATINDEX('%[^0-9|.|-|+]%','2.2')--返回0
--返回0-则为纯整数
select PATINDEX('%[^0-9]%', '2.2') --返回 非0的位置
四、从sqlserver中提取数据截取字符
1、LOCATE(substr , str ):返回子串 substr 在字符串 str 中第一次出现的位置,如果字符substr在字符串str中不存在,则返回0;
2、POSITION(substr IN str ):返回子串substr 在字符串 str 中第一次出现的位置,如果字符substr在字符串str中不存在,与LOCATE函数作用相同;
3、LEFT(str, length):从左边开始截取str,length是截取的长度;
4、RIGHT(str, length):从右边开始截取str,length是截取的长度;
5、SUBSTRING_INDEX(str ,substr ,n):返回字符substr在str中第n次出现位置之前的字符串;
6、SUBSTRING(str ,n ,m):返回字符串str从第n个字符截取到第m个字符;
7、REPLACE(str, n, m):将字符串str中的n字符替换成m字符;
8、LENGTH(str):计算字符串str的长度。
是否包含中文基本原理:字符的 unicode编码范围。
0~9 : 48~57
A~Z :65 ~ 90
a~z : 97~ 122汉字unicode编码范围:[0x4e00,0x9fa5](或十进制[19968,40869])
SELECT * FROM dbo.person WHERE UNICODE(字段) BETWEEN 19968 AND 40869
判断字符串中含有中文:
SELECT * FROM tb WHERE PATINDEX('%[啊-座]%',FieldName)>0
或: SELECT * FROM tb WHERE PATINDEX('%[吖-座]%',FieldName)>0
注意:N’%[吖-座]%’ 不适用于英文版或国外的SQLServer环境。 (<0 不含中文)
通用性最好的还是 UNICODE(字段) BETWEEN 19968 AND 40869 ,但不好做判断
-- 判断是否不含中文
IF( select top(1) PATINDEX(N'%[吖-座]%',Description) From ZZZ_Aegis_ADSN ) <=0
select 1
else
select 0
/* 判断是否包含中文,并把是否含中文作为条件来执行一些操作 */
IF( select top(1) PATINDEX('%[吖-座]%',ApplyName) From [B_00TEST] ) >0
select 1
else select 0
--这个不能作为IF的条件,只能查询到具体信息
SELECT top(1) * FROM [B_00TEST] WHERE PATINDEX(N'%[吖-座]%',ApplyName) <=0
IF( UNICODE(N'啊111') BETWEEN 19968 AND 40869 ) select 1 else select 0
IF( UNICODE(N'Abc111') BETWEEN 19968 AND 40869 ) select 1 else select 0 --中文''前面要加N
通配符:[]
> FieldName like '[a-zA-Z]%' --------- ([]指定值的范围)
> FieldName like '[^F-M]%' --------- (^排除指定范围)
可参考文章:https://www.cnblogs.com/05-hust/p/15031795.html
边栏推荐
猜你喜欢
![[cloud native] IVX low code development was introduced into Tencent map and previewed online](/img/f1/40ca06bf65fbc0097f5b61ec7d2509.png)
[cloud native] IVX low code development was introduced into Tencent map and previewed online

ACL实验演示(Huawei路由器设备配置)

Hosts this file has been set to read-only solution

Just this time! Talk about the technical solutions of distributed system in detail

【集训Day2】Torchbearer

【Unity3D】摇杆

URL跳转漏洞

2、 Topic communication principle, code implementation

天翼云Web应用防火墙(边缘云版)支持检测和拦截Apache Spark shell命令注入漏洞

【集训Day1】 Dwarves line up
随机推荐
7月30号PMP考试延期后我们应该做什么?
Redisdesktopmanager removes the upgrade prompt
Common APIs
Linux Installation mysql8.0.29 detailed tutorial
Relative path and absolute path
Detailed explanation of openwrt's feeds.conf.default
第17周自由入侵 指针练习--输出最大值
Click hijacking attack
【集训Day1】 Dwarves line up
【集训Day3】Reconstruction of roads
CCS tm4c123 new project
Mondriaans's dream (state compression DP)
Tree DP problem
A detailed explanation of throughput, QPS, TPS, concurrency and other high concurrency indicators
带你熟悉云网络的“电话簿”:DNS
Open source kaggle cat and dog data set -- used in classic CNN classification practice
来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
Tianyi cloud web application firewall (edge cloud version) supports the detection and interception of Apache spark shell command injection vulnerabilities
Machine learning by Li Hongyi 2. Regression