当前位置:网站首页>SQL Server about like operator (including the problem of field data automatically filling in spaces)
SQL Server about like operator (including the problem of field data automatically filling in spaces)
2022-06-24 10:39:00 【Cpsu】
SQL Can be used in like Keyword combined with wildcard for fuzzy query . But there are some small problems in the data type of the field .SQL The supported wildcards are :%、[ ]、[ ^ ]、_, among [ ] In Microsoft Access and SQL Sever Support collection .
1、 The percent character (%)
create table Person(
ID Varchar(20),
name Varchar(20),
age Varchar(10),
sex Varchar(2)
)
insert into Person values ('0001',' Delireba ','20','m')
insert into Person values ('0002',' Yang Mi ','20','m')
insert into Person values ('0003',' Andy ','20','m')
insert into Person values ('0004','angalbaby','20','m')
For example, you need to query name Information about the person surnamed Yang
select ID,name,age,sex
from Person
where name like ' Yang %' --% Can match any character , Include 0 individual
-- Query contains baby People's information
select ID,name,age,sex
from Person
where name like '%baby%'


Note in particular that the field type is nchar(n) The data of , If you define name The fields are nchar(20) Type, that is, the length of this field is 20, If the filled text is not filled, include Access A lot of it DBMS Will use spaces to supplement the contents of the field . For example, I want to inquire name For information about people ending in a power, use the following statement :
select ID,name,age,sex
from Person
where name like '% power '
Will not be retrieved , The wildcard should be changed to ’% power %’.
This is because nchar(n) The type is fixed length , Storage Unicode character , The data length is insufficient n Fill in the half width space in English .
And here's a rule :
When unicode data (nchar or nvarchar) And like When used together , Trailing spaces make sense . But right and wrong unicode data , Trailing spaces are meaningless . That's why char(n) The type is also a fixed length but does not match spaces .
in the light of SQL Of varchar For a detailed description of string data types, please refer to
https://blog.csdn.net/m0_46412065/article/details/105027680
2、 Underline _ wildcard
And % similar , But it only matches a single character . Of course, you can use multiple underscores to match multiple characters . For example, if you want to query the two character surname Yang
select * from
Person
where name like ' Yang _'

You also need to pay attention to underline matching nchar(n) The problem with type data . If name The data type of the field is nchar(10), Query with the above statement will not match any data .
3、[ ] and [^]
such as
select * from
Person
where age like '[0-9]0'
Will match age Field to 0~9 Any beginning between , With 0 Ending character
select * from
Person
where id like '%[^2-4]'
Will not match 2~4 Any length string at the end 
边栏推荐
猜你喜欢

leetCode-498: 對角線遍曆

JMeter interface test tool foundation - sampler (II)

JMeter interface test tool foundation - badboy tool

HBuilder制作英雄皮肤抽奖小游戏

numpy. linspace()

线程的 sleep() 方法与 wait() 方法的区别

Spark提交参数--files的使用

283. move zero

88.合并有序数组

Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
随机推荐
Image click enlargement and adaptive size in the applet rich text
Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default
23. Opencv——图像拼接项目
常用的第三方ui框架
Six states of threads
机械臂速成小指南(二):机械臂的应用
【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
24. 图像拼接大作业
The difference between the sleep () method and the wait () method of a thread
Flink checkPoint和SavePoint
Leetcode - 498: traversée diagonale
分布式事务原理以及解决分布式事务方案
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
Uniapp implementation forbids video drag fast forward
leetCode-1051: 高度检查器
tf. errors
[IEEE publication] 2022 International Conference on service robots (iwosr 2022)
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
Quick completion guide for manipulator (III): mechanical structure of manipulator
Cross domain overview, simple accumulation