Hardware and software setup

The use is similar in request 1s 8.3. Like in query conditions

LIKE- Operator for checking if a string is similar to a pattern. Analogue of LIKE in SQL.
Operator LIKE allows you to compare the value of the expression specified to the left of it with the template string specified to the right. The expression value must be of type string. If the value of the expression matches the template, the result of the operator is TRUE, otherwise it is FALSE.
The following characters in the template string are service characters and have a different meaning from the string character:
. % (percentage): sequence containing any number of arbitrary characters
. _ (underscore): one arbitrary character
. […] (in square brackets one or more characters): any single character listed inside the square brackets
An enumeration can contain ranges, such as a-z, meaning any character within the range, including the ends of the range.
. [^…] (in square brackets a negation sign followed by one or more characters): any single character other than those listed following the negation sign
Any other symbol means itself and does not carry any additional load.
If it is necessary to write one of the listed characters as itself, then it must be preceded by<Спецсимвол>. Myself<Спецсимвол>(any suitable character) is defined in the same statement after keyword SPECIAL SYMBOL.
for example, pattern “%ABC[abc]\_abc%” SPECIAL CHARACTER “\” means a substring consisting of a sequence of characters:
letters A; letters B; letters B; one digit; one of the letters a, b, c or d; underscore character; letters a; letters b; letters in.
Moreover, this sequence can be preceded by an arbitrary set of characters.

Examples of using:
Code 1C v 8.x Procedure BankTextInputEnd(Element, Text, Value, StandardProcessing)
StandardProcessing = False;
//Make a query with a search for a pattern like "%" +<Текст введенный пользователм в поле ввода> + "%"
Request = New Request;
Query.SetParameter("Name", "%" + Text + "%");
Query.Text = "SELECT
| Banks.Link
| FROM
| Directory. Banks AS Banks
|WHERE
| Banks.Name IS LIKE &Name";

Result = Request.Run();
Selection = Result.Select();
If Result.Empty() Then
//Nothing found. Here you can display a message, or something else to do :)
Otherwise
//Get results
tzResults = Result.Unload();
//Prepare a list of values ​​that will contain the found elements.
Value = New ValueList();
Value.LoadValues(tzResults.UnloadColumn("Link"));
EndIf;
EndProcedure

It is necessary that only the Names of the Main Agreement, etc., fall into the "Default Agreements":
Code 1C v 8.x Choice
When Name IS LIKE "Agreement #%" then "Agreement #" //Any line that starts with "Agreement #" qualifies
When the Name is LIKE "Main Contract%[^А-яЁё"+Symbol(33)+"-"+Symbol(126)+"№"""+Symbols.PS+Symbols.Tab+Symbols.PF+Symbols.NPP+ Symbols.VTab+"]%" then "Default Agreements" //Any string that starts with "Main Agreement" will do
Else "Other"
End As Kind Of Contract

Information taken from the site

Operator LIKE allows you to compare in a query the data of the string type to the left of the operator with the data of the string type to the right of the operator. The result of the comparison is True or False, so the comparison can be applied as a condition.

For operator LIKE special service characters are provided that are not perceived as a string:

  • "%" percent symbol: indicates the presence of any number of arbitrary characters in the string
  • "[...]" one or more characters in square brackets: indicates the presence of any (single) of the listed characters. Also, a range of characters can be specified (for example )
  • "_" underscore character: indicates the presence of any arbitrary character
  • "[^...]" negation character: denotes the presence of any single character other than those in square brackets
If you need to specify one of the above special characters for comparison, you must use the keyword "SPECIAL CHARACTER"

Features of use with various DBMS

IBM DB2"On the right of the LIKE operator, only a parameter can be located. Template characters are only "_" (underscore meaning any character) and "%" (percentage meaning a sequence of any characters).
In the case of using a DBMS " PostgreSQL" or " Oracle Database"special characters "square brackets [...]" are accepted only if specified by the text in the request, and are NOT passed as a parameter to the request.

Thus, in the file base, special characters will always be perceived in the same way, and in different ways, depending on the DBMS used in the client-server version.

Example: select products containing the symbol "%" in the name

CHOOSE | Ref. Link | FROM | Spravochnik.Nomenclature HOW ref | WHERE | Ref Name LIKE "%\%" SPECIAL CHARACTER "\"

Example: select products whose name starts with the word "Tank"

CHOOSE | Ref. Link | FROM | Spravochnik.Nomenclature HOW ref | WHERE | Ref Name LIKE "Buck%"

Example: select products whose name ends with a number

CHOOSE | Ref. Link | FROM | Spravochnik.Nomenclature HOW ref | WHERE | Ref Name LIKE "%"

Despite all the shortcomings, text field search is still one of the most popular. We can find string data types everywhere - names, account numbers, addresses, and other information can be stored in this format. In queries in the built-in 1C language, for the convenience of developers, a special operator "LIKE" is used. This is one of the most used commands, so without a thorough knowledge of its syntax and capabilities, it will be difficult for a programmer to work.

Using the "LIKE" operator

Before you put any operator into practice, you need to clearly understand its purpose, places of application and syntax. The purpose of using "LIKE" in the 1C query is to check for satisfaction of the condition presented as a template. The return value is a boolean, true or false, indicating whether the specified condition is met. The LIKE operator can be used in several places in a query:

  • In the block of conditions indicated by the keyword "WHERE";
  • In the construction Choice When Then Else End;
  • Directly in the selection fields, as a result of field comparison.

The syntax of the check is always the same and consists of 3 links. On the left is the text value that is being checked, then the “LIKE” operator itself, and on the right is the template that is being checked. For quick and convenient templating, there are special symbols that make development easier:

  1. "%" is a sequence of any characters of arbitrary length. Used to search individual words or numbers in a string;
  2. "_" is any single character. Designed to indicate the presence of one character;
  3. "[...]" is a sequence of characters to compare with a sign in the string. With the help of such a pattern, a match is checked for any of the characters listed in brackets. You can also specify a range of numbers or letters ([a-g], );
  4. “[^…]” is the opposite of the previous pattern. The difference between the character specified in the string and those listed in brackets is checked.

To better assimilate and understand the principles of creating correct templates, let's look at some examples that are often encountered in the life of developers. The first one is when we need to select from the nomenclature reference book all positions in the names of which the word "CUTTER" occurs. In this case, we need to use LIKE in the query conditions:

SELECT Nomenclature.Name AS Name FROM Directory.Nomenclature AS Nomenclature WHERE Nomenclature.Name LIKE "%CUTTER%"

If we remove both "%" characters, then the query will show the nomenclature, in which the name completely matches the one indicated in quotes. If we leave the template "CUTTER%" or "%CUTTER", then the result will be a list of nomenclature ending or starting, respectively, with a given combination of characters.


Let's analyze a problem that can confuse novice programmers who do not know the query syntax. Let's say you need to find all the nomenclature, in the name of which there is a symbol "%". Especially for cases when you need to search for reserved characters, there is a "special character" operator. As a special character, you can use #, \, /, ~ and other characters, after which any reserved characters will simply denote a character.

SELECT Nomenclature.Name AS Name FROM Directory.Nomenclature AS Nomenclature WHERE Nomenclature.Name LIKE "%#%" SPECIAL CHARACTER "#"

If you need to use a parameter in the search, then the variable in the query with the LIKE parameter is used by adding. Remember that the parameter must be a string type or you will need to convert it to a string in the request. This is a rather complicated operation and it is better to exclude it in advance.

SELECT Nomenclature.Name AS Name FROM Directory.Nomenclature AS Nomenclature WHERE Nomenclature.Name LIKE "%" + &name + "%"

The LIKE function is applicable in all versions of the platform, starting from 8, and due to its applicability, 1C developers will not want to change it. Of course, text search always depends on the accuracy of entering the name, but it still remains one of the most common. In this regard, professional 1C developers need to study the use of LIKE with all its nuances.

Sometimes a situation arises when in 1C 8.3 or 8.2 you need to make a selection, for example, from a directory of all elements that have the word "glaze" in their names. Or, from the directory, select all counterparties whose last names contain the word "Ivan". In general, check some string value.

For this, there is an operator in requests 1C 8.3 and 8.2 - “Like”. It is used, respectively, in the conditions:

Get 267 1C video lessons for free:

How to use templates in 1C requests?

To form a selection condition, you need to pass a template as a parameter. To create a template, there are so-called service symbols.

For example, the "%" character allows any sequence of arbitrary characters:

There are other special characters:

  • % (percentage) - allows any sequence of arbitrary characters;
  • _ (underscore) - any single character;
  • […] is one arbitrary character from those listed inside the brackets. In addition to enumerating characters, you can use ranges. Example: a-o;
  • [^…] – the same as the previous one, but vice versa. The "^" sign means negation.

The LIKE operator in a query checks string values ​​from tables for similarity to a pattern.
It is used as follows to the left of this operator is the string to be checked, and to the right is the template.

After checking, it returns True or False, respectively, it is actively used in conditions.
The following service symbols are used to create a template:

  • % (percentage) - a sequence containing any number of arbitrary characters
  • _ (underscore) - one arbitrary character
  • […] (one or more characters in square brackets) - any single character listed inside square brackets
    Also, in addition to various characters, ranges can be used, for example a-z(A-z), which means that there is an arbitrary character included in the range, including the ends of the range.
  • [^…] (in square brackets a negation sign followed by one or more characters) - any single character, except for those listed after the negation sign

The remaining characters are used for their intended purpose.
If one of the above service characters must be transmitted as a character, then it must be preceded by<Спецсимвол>. Myself<Спецсимвол>(any suitable character) is defined in the same statement after the SPECIAL CHARACTER keyword.
For example, the pattern “%ABC[abc]\_abc%” SPECIAL CHARACTER “\” means a substring consisting of a sequence of characters:
letters A; letters B; letters B; one digit; one of the letters a, b, c or d; underscore character; letters a; letters b; letters in.
Moreover, this sequence can be preceded by an arbitrary set of characters.

Procedure SelectContractContainingInNameText(mText)
//In the request, we will use a template like "%" + mText + "%" Request = New Request; Query.SetParameter("Name", "%" + Text + "%"); Query.Text = "SELECT | Agreements.Reference, | Agreements.Owner |FROM | Directory.AgreementsofCounterparties AS Agreements | |WHERE | Agreements.Name LIKE &Name"; Result = Request.Run(); Selection = Result.Select(); Report("Agreements containing in the name: " + mText + " have the following Counterparties"); While Selection.Next() Loop Report("Account: " + Selection.Owner + "; Contract: " + Selection.Reference EndIf; EndProcedure

Liked the article? Share with friends!
Was this article helpful?
Yes
Not
Thanks for your feedback!
Something went wrong and your vote was not counted.
Thank you. Your message has been sent
Did you find an error in the text?
Select it, click Ctrl+Enter and we'll fix it!