当前位置:网站首页>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结尾的任意长度的字符串
边栏推荐
- El table Click to add row style
- 415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)
- Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
- Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction
- js代理模式
- About thinkphp5, use the model save() to update the data prompt method not exist:think\db\query- & gt; Error reporting solution
- 被困英西中学的师生安全和食物有保障
- canvas无限扫描js特效代码
- 414-二叉树的递归遍历
- 观察者模式
猜你喜欢

LeetCode: 240. Search 2D matrix II

机器学习——感知机及K近邻

canvas管道动画js特效

GeoGebra 实例 时钟

Cicflowmeter source code analysis and modification to meet requirements

Thinkphp5 multi language switching project practice

indexedDB本地存储,首页优化

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

JS singleton mode

Nvisual digital infrastructure operation management software platform
随机推荐
el-table点击添加行样式
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
How do novices choose the grade of investment and financial products?
canvas掉落的小球重力js特效动画
买的长期理财产品,可以转短吗?
Open Oracle server under Linux to allow remote connection
js代理模式
Thinkphp5 clear the cache cache, temp cache and log cache under runtime
Ora-28000 error after upgrading Oracle 12C to 19C
《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
Why is JSX syntax so popular?
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
PostgreSQL DBA快速入门-通过源码编译安装
Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
Five heart matchmaker
SQL statistics of users logged in for N consecutive days
Observer mode
居家办公如何管理数据中心网络基础设施?
Record the range of data that MySQL update will lock