当前位置:网站首页>SQL Sever关于like操作符(包括字段数据自动填充空格问题)
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
2022-06-24 09:40:00 【Cpsu】
SQL中可以使用like关键字结合通配符进行模糊查询。但在字段的数据类型上会存在一些小问题。SQL支持的通配符有:%、[ ]、[ ^ ]、_,其中[ ]在微软的Access和SQL Sever支持集合。
1、百分号通配符(%)
create table Person(
ID Varchar(20),
name Varchar(20),
age Varchar(10),
sex Varchar(2)
)
insert into Person values ('0001','迪丽热巴','20','m')
insert into Person values ('0002','杨幂','20','m')
insert into Person values ('0003','杨紫','20','m')
insert into Person values ('0004','angalbaby','20','m')
比如需要查询name中姓杨的人的信息
select ID,name,age,sex
from Person
where name like '杨%' --%可以匹配任意个字符,包括0个
--查询含有baby的人的信息
select ID,name,age,sex
from Person
where name like '%baby%'


特别注意字段类型为nchar(n)的数据,如果定义的name字段是nchar(20)类型的即该字段的长度为20,如果填入的文本没有填满则包括Access在内的许多DBMS会用空格来补充字段的内容。比如我想查询name以幂结尾的人的信息使用如下语句:
select ID,name,age,sex
from Person
where name like '%幂'
则会检索不出来,此时通配符应该改为’%幂%’。
这是因为nchar(n)类型是固定长度,存储Unicode字符,在数据长度不足n的时候补英文半角空格。
而在这里有一个规定:
当unicode数据(nchar或nvarchar)与like 一起使用时,尾随空格有意义。但对非unicode数据,尾随空格无意义。这也是为什么char(n)类型也是固定长度但是却不会匹配出空格的原因。
针对SQL的varchar等字符串数据类型的详细介绍可以参考
https://blog.csdn.net/m0_46412065/article/details/105027680
2、下划线 _ 通配符
与%类似,但它只匹配单个字符。当然可以使用多个下划线来匹配多个字符。比如想查询姓杨且为两个字的
select * from
Person
where name like '杨_'

下划线匹配时同样需要注意nchar(n)类型数据的问题。如果name字段的数据类型是nchar(10),使用上述语句查询则匹配不到任何数据。
3、[ ] 和 [^]
比如
select * from
Person
where age like '[0-9]0'
会匹配age字段以0~9之间任意一个开头,以0结尾的字符
select * from
Person
where id like '%[^2-4]'
会匹配不以2~4结尾的任意长度的字符串
边栏推荐
- How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
- 上升的气泡canvas破碎动画js特效
- Handling method of Oracle data file header SCN inconsistency
- Why is JSX syntax so popular?
- Amendment to VPP implementation policy routing
- Ora-16038 ora-19502 ora-00312 troubleshooting
- Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction
- In depth study paper reading target detection (VII) Chinese English Bilingual Edition: yolov4 optimal speed and accuracy of object detection
- 简单的价格表样式代码
- Regular matching mobile number
猜你喜欢

Canvas draw picture

How to standardize data center infrastructure management process

Getting user information for applet learning (getuserprofile and getUserInfo)

Queue queue

411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)

ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?

413-二叉树基础

小程序学习之获取用户信息(getUserProfile and getUserInfo)

如何提高网络基础设施排障效率,告别数据断档?

二叉树第一部分
随机推荐
数组无缝滚动demo
el-table表格的拖拽 sortablejs
Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
SSH Remote Password free login
Oracle viewing data file header SCN information
About thinkphp5, use the model save() to update the data prompt method not exist:think\db\query- & gt; Error reporting solution
买的长期理财产品,可以转短吗?
Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
Geogebra instance clock
impdp导schema报ORA-31625异常处理
Symbol.iterator 迭代器
请问有国内靠谱低手续费的期货开户渠道吗?网上开户安全吗?
Distributed | how to make "secret calls" with dble
PostgreSQL
How to standardize data center infrastructure management process
微信小程序学习之 实现列表渲染和条件渲染.
canvas管道动画js特效
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
Internet of things? Come and see Arduino on the cloud
Go language development environment setup +goland configuration under the latest Windows