---
title: SQL Wildcards
description: "## SQL Wildcard Characters – SQL Орлуулгын тэмдэгтүүд A wildcard character is used to substitute one or more characters in a string. Орлуулгын тэмдэг нь нэг эсвэл хэд хэдэн тэмдэгтийг орлуулахад ашиглагддаг. Wildcard characters are used with the `LIKE` operator. The `LIKE` operator is…"
url: "https://apprentice.mn/docs/sql-wildcards/"
updated: "2024-07-18"
category: SQL
---

# SQL Wildcards

## SQL Wildcard Characters – SQL Орлуулгын тэмдэгтүүд

A wildcard character is used to substitute one or more characters in a string.

Орлуулгын тэмдэг нь нэг эсвэл хэд хэдэн тэмдэгтийг орлуулахад ашиглагддаг.

Wildcard characters are used with the `LIKE` operator. The `LIKE` operator is used in a `WHERE` clause to search for a specified pattern in a column.

Орлуулгын тэмдгүүд нь `LIKE` оператортой хамт ашиглагддаг. `LIKE` оператор нь баганад тодорхойлсон зүйлийг хайхад `WHERE` нөхцөлд ашиглагддаг.

### Example – Жишээ

Return all customers that starts with the letter ‘a’:

‘a’ үсгээр эхэлдэг бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘a%’;

---

## Wildcard Characters – Орлуулгын тэмдэгүүд

| Symbol Тэмдэгт | Description Тайлбар |
| --- | --- |
| % | Represents zero or more characters Тэг эсвэл олон тэмдэгтүүд орлоно |
| _ | Represents a single character Нэг тэмдэгт орлоно |
| \[\] | Represents any single character within the brackets \* Хаалтад байх нэг тэмдэгийг орлоно |
| ^ | Represents any character not in the brackets \* Хаалтад байгаагаас бусад тэмдэгтийг орлоно |
| – | Represents any single character within the specified range \* Тодорхойлсон хэсгийн хүрээны нэг тэмдэгийг орлоно |
| {} | Represents any escaped character \*\* Зайтай тэмдэгтүүдийг орлоно  |

\* Not supported in PostgreSQL and MySQL databases.

\* PostgreSQL болон MySQL мэдээллийн сангуудад хэрэглэдэггүй.

\*\* Supported only in Oracle databases.

\*\* Зөвхөн Oracle мэдээллийн сангуудад хэрэглэдэг.

---

## Demo Database – Жишээ мэдээллийн сан

Below is a selection from the **Customers** table used in the examples:

Доорх **Харилцагчид** хүснэгтийг жишээ болгож ашиглав:

| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |

## Using the % Wildcard – Орлуулгын % тэмдэгтийн ашиглах

The `%` wildcard represents any number of characters, even zero characters.

`%` орлуулгын тэмдэг нь ямар ч тооны тэмдэгтийг төлөөлнө, хоосон тэмдэгтийг ч бас.

### Example – Жишээ

Return all customers that ends with the pattern ‘es’:

‘es’-ээр төгсдөг бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘%es’;

### Example – Жишээ

Return all customers that *contains* the pattern ‘mer’:

‘mer’ агуулсан бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘%mer%’;

---

## Using the _ Wildcard – Орлуулгын _ тэмдэгтийн ашиглах

The `_` wildcard represents a single character.

`_` орлуулгын тэмдэг нь нэг тэмдэгтийг төлөөлнө.

It can be any character or number, but each `_` represents one, and only one, character.

Доогуур зураас ашиглаж хэдэн ч тэмдэгт эсвэл тоог илэрхийлж болно, гэхдээ нэг `_` зөвхөн ганц л тэмдэгтийг төлөөлнө.  

### Example – Жишээ

Return all customers with a `City` starting with any character, followed by “ondon”:

Хотын нэр нь ямар нэгэн тэмдэгтээр эхлээд “ondon” гэж үргэлжлэдэг бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE City LIKE ‘_ondon’;

### Example – Жишээ

Return all customers with a `City` starting with “L”, followed by any 3 characters, ending with “on”:

Хотын нэр нь “L” үсгээр эхлээд, ямар нэгэн 3 тэмдэгтээр үргэлжилж, “on” үсгээр төгсдөг бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE City LIKE ‘L___on’;

---

## Using the \[\] Wildcard – Орлуулгийн \[\] тэмдэгт ашиглах

The `[]` wildcard returns a result if *any* of the characters inside gets a match.

`[]` орлуулгын тэмдэг нь доторх тэмдэгтүүдээс аль нэг нь таарч байвал үр дүнг буцаана.

### Example – Жишээ

Return all customers starting with either “b”, “s”, or “p”:

“b”, “s”, эсвэл “p” үсгээр эхэлдэг бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘\[bsp\]%’;

---

## Using the `-` Wildcard – Орлуулгын `-` тэмдгийг ашиглах

The `-` wildcard allows you to specify a range of characters inside the `[]` wildcard.

`-` орлуулгын тэмдэг нь `[]` орлуулгын тэмдэг дотор байх тэмдэгтүүдийн хүрээг тодорхойлоход ашиглагддаг.

### Example – Жишээ

Return all customers starting with “a”, “b”, “c”, “d”, “e” or “f”:

“a”, “b”, “c”, “d”, “e” эсвэл “f” үсгээр эхэлдэг бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘\[a-f\]%’;

---

## Combine Wildcards – Орлуулгын тэмдгүүдийг хослуулах

Any wildcard, like `%` and `_` , can be used in combination with other wildcards.

`%` болон `_` зэрэг орлуулгын тэмдгүүдийг бусад орлуулгын тэмдгүүдтэй хослуулан ашиглаж болно.

### Example – Жишээ

Return all customers that starts with “a” and are at least 3 characters in length:

“a” үсгээр эхэлж, урт нь дор хаяж 3 тэмдэгттэй бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘a__%’;

### Example – Жишээ

Return all customers that have “r” in the second position:

2-р байрлалд “r” үсэгтэй бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE CustomerName LIKE ‘_r%’;

---

## Without Wildcard – Орлуулгын тэмдэгтгүйгээр

If no wildcard is specified, the phrase has to have an exact match to return a result.

Хэрэв орлуулгын тэмдэгт заагаагүй бол, тодорхойлсон тодорхойлолт нь яг таарч байж үр дүнг буцаана.

### Example – Жишээ

Return all customers from Spain:

Испани бүх харилцагчдыг буцаана:

> SELECT \* FROM Customers
> WHERE Country LIKE ‘Spain’;

---

## Microsoft Access Wildcards – Microsoft Access орлуулгын тэмдгүүд

The Microsoft Access Database has some other wildcards:

Microsoft Access мэдээллийн санд бусад орлуулгын тэмдгүүд байдаг:

| Symbol Тэмдэгт | Description Тайлбар | Example Жишээ |
| --- | --- | --- |
| \* | Represents zero or more characters Хоосон эсвэл ямар нэг тэмдэгт төлөөлөх | bl\* finds bl, black, blue, and blob |
| ? | Represents a single character Ганц тэмдэгт төлөөлөх | h?t finds hot, hat, and hit |
| \[\] | Represents any single character within the brackets Хаалтан дотор байх тэмдэгтүүдээс ганц тэмдэгтийг төлөөлнө | h\[oa\]t finds hot and hat, but not hit |
| ! | Represents any character not in the brackets Хаалтан дотор байхгүй тэмдэгтүүдийг төлөөлнө | h\[!oa\]t finds hit, but not hot and hat |
| – | Represents any single character within the specified range Хаалтан доторх тэмдэгийн хүрээн дэх ганц тэмдэгтийг төлөөлнө | c\[a-b\]t finds cat and cbt |
| # | Represents any single numeric character Нэг тоон тэмдэгтийг төлөөлнө | 2#5 finds 205, 215, 225, 235, 245, 255, 265, 275, 285, and 295 |

        [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-wildcards%2F&linkname=SQL%20Wildcards](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-wildcards%2F&linkname=SQL%20Wildcards)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-wildcards%2F&linkname=SQL%20Wildcards](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-wildcards%2F&linkname=SQL%20Wildcards)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-wildcards%2F&linkname=SQL%20Wildcards](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-wildcards%2F&linkname=SQL%20Wildcards)[https://www.addtoany.com/share](https://www.addtoany.com/share)
