当前位置:网站首页>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结尾的任意长度的字符串
边栏推荐
- Operator details
- SQL statistics of users logged in for N consecutive days
- 如何提高网络基础设施排障效率,告别数据断档?
- canvas无限扫描js特效代码
- 操作符详解
- PHP file lock
- GeoGebra 实例 时钟
- [Eureka source code analysis]
- How do novices choose the grade of investment and financial products?
- Cicflowmeter source code analysis and modification to meet requirements
猜你喜欢

美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架

Geogebra instance clock

canvas掉落的小球重力js特效动画

时尚的弹出模态登录注册窗口

二叉樹第一部分

In depth study paper reading target detection (VII) Chinese English Bilingual Edition: yolov4 optimal speed and accuracy of object detection

LeetCode: 240. Search 2D matrix II

How to standardize data center infrastructure management process

英伟达这篇CVPR 2022 Oral火了!2D图像秒变逼真3D物体!虚拟爵士乐队来了!

顶刊TPAMI 2022!基于不同数据模态的行为识别:最新综述
随机推荐
Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
canvas无限扫描js特效代码
411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
p5.js千纸鹤动画背景js特效
Detailed explanation of PHP singleton mode
JS singleton mode
Indexeddb local storage, homepage optimization
Nvisual digital infrastructure operation management software platform
NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
机器学习——感知机及K近邻
el-table点击添加行样式
物联网?快来看 Arduino 上云啦
PostgreSQL DBA快速入门-通过源码编译安装
413 binary tree Foundation
Open Oracle server under Linux to allow remote connection
PHP encapsulates a file upload class (supports single file and multiple file uploads)
[input method] so far, there are so many Chinese character input methods!
Impdp leading schema message ora-31625 exception handling
Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
[custom endpoint and implementation principle]