← Back to MySQL Course | Chapter 12: String Functions | Lesson 5 of 7

TRIM LTRIM RTRIM

TRIM किसी word के edges से extra spaces झाड़ने जैसा है। LTRIM left side साफ करता है और RTRIM right side।
Syntax
sql
TRIM([BOTH | LEADING | TRAILING] [remove_string FROM] string)
LTRIM(string)
RTRIM(string)

TRIM का Introduction

TRIM किसी string की शुरुआत और अंत दोनों से whitespace strip करता है साथ ही बीच के किसी भी spaces को untouched छोड़ते हुए, जो user-typed values compare या store करते समय मायने रखता है जो अक्सर accidental padding रखती हैं।

उदाहरण: Introduction to TRIM

sql
SELECT TRIM('   Hello World   ') AS trimmed;

LTRIM इस्तेमाल करना

LTRIM किसी string के left edge से सिर्फ leading whitespace हटाता है, trailing spaces intact रखते हुए। यह तब उपयोगी है जब आप specifically यह normalize करना चाहते हों कि एक value कैसे शुरू होती है इसके बाद कुछ भी बदले बिना।

उदाहरण: Using LTRIM

sql
SELECT LTRIM('   Hello World   ') AS left_trimmed;

RTRIM इस्तेमाल करना

RTRIM LTRIM को mirror करता है लेकिन इसके बजाय right edge पर काम करता है, trailing spaces strip करते हुए साथ ही string के आगे के हिस्से को अकेला छोड़ते हुए। यह fixed-width import data साफ करते समय common है जो values को trailing blanks से pad करता है।

उदाहरण: Using RTRIM

sql
SELECT RTRIM('   Hello World   ') AS right_trimmed;

Specific Characters साफ करना

TRIM सिर्फ whitespace तक limited नहीं है — LEADING, TRAILING, या BOTH keyword pass करना आपको एक specific character strip करने देता है, जैसे imported text से सिर्फ blank space के बजाय stray leading zeros या trailing commas हटाना।

उदाहरण: Cleaning Specific Characters

sql
SELECT TRIM(LEADING '0' FROM '000123') AS no_leading_zeros;
SELECT TRIM(TRAILING ',' FROM 'item,item,') AS no_trailing_comma;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Form Validation और Cleaning

Form input stray spaces का एक classic source है, क्योंकि users अक्सर एक email या username type करने से पहले या बाद में एक space add कर देते हैं। save करने से पहले values trim करना उन subtle bugs से बचाता है जहाँ '[email protected]' और '[email protected] ' को दो अलग accounts माना जाए।

उदाहरण: Form Validation and Cleaning

sql
CREATE TABLE users (id INT, email TEXT);
INSERT INTO users VALUES (1, '[email protected] '), (2, '[email protected]');
SELECT * FROM users WHERE TRIM(email) = '[email protected]';
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि TRIM किसी string के बीच के spaces हटा देगा, जबकि यह उन्हें सिर्फ सिरों से हटाता है।
  2. बिना TRIM(BOTH x FROM col) के दूसरे characters trim करने की कोशिश करना।
  3. यह भूल जाना कि TRIM stored value नहीं बदलता जब तक आप UPDATE इस्तेमाल न करें।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.