当前位置:网站首页>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结尾的任意长度的字符串
边栏推荐
- PostgreSQL
- CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算
- Handling method of Oracle data file header SCN inconsistency
- Ora-28000 error after upgrading Oracle 12C to 19C
- 涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
- Wechat applet learning to achieve list rendering and conditional rendering
- Five heart matchmaker
- el-table点击添加行样式
- p5.js实现的炫酷交互式动画js特效
- 美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
猜你喜欢

Operator details

植物生长h5动画js特效

Cicflowmeter source code analysis and modification to meet requirements

Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction

p5.js千纸鹤动画背景js特效

Programming questions (continuously updated)

2021-08-17

Basic operations on binary tree

《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测

有关二叉树 的基本操作
随机推荐
[custom endpoint and implementation principle]
Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
Floating point notation (summarized from cs61c and CMU CSAPP)
Recursive traversal of 414 binary tree
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
Programming questions (continuously updated)
Groovy obtains Jenkins credentials through withcredentials
Why is JSX syntax so popular?
生产者/消费者模型
微信小程序學習之 實現列錶渲染和條件渲染.
操作符详解
413-二叉树基础
Thinkphp5 clear the cache cache, temp cache and log cache under runtime
Use of vim
Thinkphp5 multi language switching project practice
p5.js千纸鹤动画背景js特效
Record the range of data that MySQL update will lock
Error reading CSV (TSV) file
How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
Impdp leading schema message ora-31625 exception handling