What is the meaning of LIKE or _% used in SELECT statements?

Asked 2 years ago, Updated 2 years ago, 43 views

The following formula is used for the conditions after WHERE:
As an example, there is no explanation for the SELECT statement, so I don't know the meaning of the expression and how to interpret it in the SELECT statement and how to interpret it.

LIKE "_Vegetable %"
(Meaning) A string containing "vegetables"._ is a single character, % is an arbitrary character wildcard

What do you mean?

sql

2022-09-30 21:20

3 Answers

Like is used to search for patterns in a string, such as column name like 'pattern character'.

_ is any single character and % is any string of "0 or more characters", so 'like'_vegetable%' searches for one character before 'vegetable', and 'vegetable' after 'vegetable'.

create table test1(
  comment varchar(255)
);
insert into test1 values('I love vegetables');
insert into test1 values('I love hot vegetables');
insert into test 1 values ('hot vegetables');
insert into test 1 values ('I love pesticide-free vegetables');

select * from test1 where comment like '_vegetable %';

Run Results

I love hot vegetables
hot vegetable
  • "I love vegetables" is not selected because there is no letter in front of "vegetables."
  • "I love pesticide-free vegetables" is not selected because the front of "vegetables" is three letters long.

http://sqlfiddle.com/#!9/184e0/1


2022-09-30 21:20

_ and % are wildcards.

Since _ represents any single character, LIKE "_vegetables" includes
vegetables, raw vegetables, etc. % represents any string of at least 0 characters, and LIKE "vegetable %" is affected by such things as "vegetable" or "vegetable juice."

https://technet.microsoft.com/ja-jp/library/ms187489(v=sql.105).aspx


2022-09-30 21:20

Is there a use case?

SELECT* FROM tblName WHERE fieldName LIKE'_Vegetable %'

SELECT [Field you want to display] 
FROM [Table Name] 
WHERE [Field to be searched] LIKE '[Search Pattern]'

[Search Patterns] can use wildcards.
The wildcard is described by someone else, so please refer to it for more information.


2022-09-30 21:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.