---
title: Text.Contains – ตรวจสอบว่าข้อความมีส่วนประกอบที่ต้องการหรือไม่
url: https://www.thepexcel.com/functions/power-query/text-functions/text-contains/
type: function-explainer
program: Power Query
syntax: "Text.Contains(text, substring, comparer)"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 8
  difficulty: 3
  usefulness: 8
---

# Text.Contains – ตรวจสอบว่าข้อความมีส่วนประกอบที่ต้องการหรือไม่

> Text.Contains ใช้สำหรับตรวจสอบว่าข้อความที่กำหนด (text) มีข้อความย่อยที่ต้องการค้นหา (substring) อยู

## คำอธิบาย

Text.Contains ใช้สำหรับตรวจสอบว่าข้อความที่กำหนด (text) มีข้อความย่อยที่ต้องการค้นหา (substring) อยู่ภายในหรือไม่ ส่งคืนค่า true หรือ false

## Syntax

```excel
Text.Contains(text, substring, comparer)
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| text | Yes | text |  | ข้อความที่ต้องการค้นหา (สามารถเป็น null ได้) |
| substring | Yes | text |  | ส่วนข้อความย่อยที่ต้องการค้นหาภายในข้อความแรก |
| comparer | No | function | Comparer.Ordinal (case-sensitive) | ฟังก์ชันเปรียบเทียบ เช่น Comparer.OrdinalIgnoreCase สำหรับการค้นหาแบบไม่สนใจตัวพิมพ์เล็กใหญ่ |

## เคสการใช้งาน

### กรองข้อมูลที่มีคำเฉพาะ

กรองรายการสินค้าที่มีคำว่า "Discount" หรือ "Promotion" อยู่ในชื่อ

### ตรวจสอบประเภทไฟล์

เช็คว่าชื่อไฟล์มีนามสกุล ".xlsx" หรือ ".csv" หรือไม่

### จัดหมวดหมู่ข้อความ

ถ้าพบคำว่า "Apple" ในข้อความ ให้จัดอยู่ในหมวด "Fruit"

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ค้นหาข้อความแบบ Case-Sensitive

```excel
= Text.Contains("Hello World", "Hello")
```

**ผลลัพธ์:** `true`

ข้อความ "Hello World" มีส่วน "Hello" อยู่ ดังนั้นคืนค่า true

### 2. ตัวอย่างที่ 2: ค้นหาแบบ Case-Sensitive ไม่พบ

```excel
= Text.Contains("Hello World", "hello")
```

**ผลลัพธ์:** `false`

ค้นหา "hello" (ตัวพิมพ์เล็ก) ในข้อความ "Hello World" ไม่พบเพราะ H ตัวพิมพ์ใหญ่ ดังนั้นคืนค่า false

### 3. ตัวอย่างที่ 3: ค้นหาแบบ Case-Insensitive

```excel
= Text.Contains("Hello World", "hello", Comparer.OrdinalIgnoreCase)
```

**ผลลัพธ์:** `true`

ใช้ Comparer.OrdinalIgnoreCase เพื่อค้นหาแบบไม่สนใจตัวพิมพ์เล็กใหญ่ ดังนั้น "hello" พบใน "Hello World" และคืนค่า true

### 4. ตัวอย่างที่ 4: กรองตารางค้นหารหัสสินค้า

```excel
= let
    Products = #table(
      type table [ProductCode = text, ProductName = text, Price = number],
      {
        {"PROD-001", "Laptop", 25000},
        {"PROD-002", "Mouse", 500},
        {"PART-003", "Keyboard", 1200},
        {"PROD-004", "Monitor", 8000},
        {"PART-005", "USB Cable", 150}
      }
    ),
    FilteredProducts = Table.SelectRows(
      Products,
      each Text.Contains([ProductCode], "PROD")
    )
  in
    FilteredProducts
```

**ผลลัพธ์:** `ตารางที่มี 3 แถว: PROD-001 (Laptop), PROD-002 (Mouse), PROD-004 (Monitor)`

ใช้ Text.Contains กับ Table.SelectRows เพื่อกรองเฉพาะสินค้าที่รหัสมี "PROD" เท่านั้น

### 5. ตัวอย่างที่ 5: ค้นหาบัญชีลูกค้า (ไม่สนใจตัวพิมพ์)

```excel
= let
    Accounts = #table(
      type table [Account = text, Balance = number],
      {
        {"us-2004", 580},
        {"ca-8843", 280},
        {"pa-1274", 90},
        {"pty-507", 110}
      }
    ),
    FindAccounts = Table.SelectRows(
      Accounts,
      each Text.Contains([Account], "A-", Comparer.OrdinalIgnoreCase) or
           Text.Contains([Account], "7", Comparer.OrdinalIgnoreCase)
    )
  in
    FindAccounts
```

**ผลลัพธ์:** `ตารางที่มี 3 แถว: ca-8843, pa-1274, pty-507`

ค้นหาบัญชีที่มี "A-" หรือ "7" โดยไม่สนใจตัวพิมพ์เล็กใหญ่ ใช้ OR logic เพื่อค้นหาหลายเงื่อนไข

## หมายเหตุเพิ่มเติม

- ใช้ Comparer.OrdinalIgnoreCase เมื่อต้องการค้นหาแบบไม่สนใจตัวพิมพ์เล็กใหญ่ (case-insensitive)

- รวม Text.Contains กับ Table.SelectRows เพื่อกรองแถวตารางตามเงื่อนไขข้อความ

- ใช้ OR (or) หรือ AND (and) เพื่อสร้างเงื่อนไขการค้นหาหลายๆ แบบ เช่น ค้นหา "A-" หรือ "7"

- ตรวจสอบค่า null ก่อนใช้ Text.Contains เพื่อหลีกเลี่ยง error ที่ไม่คาดคิด

- ถ้าต้องการค้นหาด้วย Regular Expression ให้ใช้ Text.Match แทน Text.Contains

- Text.Contains ไม่รองรับ Wildcard ควรค้นหาแบบตัวอักษรที่ต้องการจริงๆ

## คำถามที่พบบ่อย

**Q: Text.Contains เหมือนกับ Excel FIND หรือ SEARCH ไหม?**

เหมือนกันบ้าง แต่ต่างกันตรงที่ FIND ของ Excel คืนค่าเป็นเลข (ตำแหน่งที่พบ) ขณะที่ Text.Contains คืนค่า true/false ถ้าต้องการตำแหน่ง ใน Power Query ให้ใช้ Text.PositionOf แทน

**Q: Text.Contains รองรับ Wildcard เช่น * หรือ ? ไหม?**

ไม่รองรับ Text.Contains ทำการค้นหาแบบตรงตัว (Literal) เท่านั้น ถ้าต้องการ Wildcard ลองใช้ Text.Match กับ Regular Expression แทน

**Q: ถ้า text เป็น null จะเกิดอะไร?**

Text.Contains จะคืนค่า null ถ้าพารามิเตอร์แรก (text) เป็น null ต่างจากการส่ง substring ที่เป็น null ซึ่งจะเกิด Error

**Q: Comparer.OrdinalIgnoreCase ต่างจาก Comparer.FromCulture ยังไง?**

Ordinal ใช้การเปรียบเทียบแบบตัวอักษรบริสุทธิ์ ส่วน FromCulture ใช้กฎการเปรียบเทียบของวัฒนธรรม/ภาษาที่กำหนด เช่นการจัดการตัวอักษรพิเศษในภาษาต่างๆ

**Q: ทำไมต้องใช้ Text.Contains กับ Table.SelectRows?**

เพราะ Table.SelectRows ต้องการให้ส่งค่า logical (true/false) เพื่อตัดสินใจว่าแถวไหนจะเก็บไว้ Text.Contains คืนค่า true/false พอดี ทำให้เป็นความเชื่อมโยงที่สมบูรณ์

## ฟังก์ชันที่เกี่ยวข้อง

- [Text.Lower – แปลงข้อความเป็นตัวพิมพ์เล็ก](https://www.thepexcel.com/functions/power-query/text-functions/text-lower/)
- [Text.Upper – แปลงข้อความเป็นตัวอักษรใหญ่](https://www.thepexcel.com/functions/power-query/text-functions/text-upper/)
- [Text.PositionOf – หาตำแหน่งข้อความ](https://www.thepexcel.com/functions/power-query/text-functions/text-positionof/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)
- text-match

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn - Text.Contains](https://learn.microsoft.com/en-us/powerquery-m/text-contains) _(official)_
- [Microsoft Learn - Comparer Functions](https://learn.microsoft.com/en-us/powerquery-m/comparer-ordinal) _(official)_
- [Power Query M Function Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/text-functions/text-contains/](https://www.thepexcel.com/functions/power-query/text-functions/text-contains/)_
