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

LENGTH और CHAR_LENGTH

LENGTH और CHAR_LENGTH दो rulers हैं: एक बताता है कि किसी चीज़ ने कितने bytes इस्तेमाल किए और दूसरा visible characters count करता है, जो special letters के लिए अलग हो सकते हैं।
Syntax
sql
LENGTH(string)        -- length in bytes
CHAR_LENGTH(string)   -- length in characters

Length को समझना

MySQL के पास string sizes measure करने के दो main तरीके हैं, और गलत वाला चुनना non-ASCII text के साथ subtle bugs का कारण बन सकता है। एक byte size measure करता है, और दूसरा character count, और multi-byte characters शामिल होते ही वे अलग हो जाते हैं।

उदाहरण: Understanding Length

sql
SELECT LENGTH('cafe test') AS byte_length, CHAR_LENGTH('cafe test') AS char_length;

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

Bytes बनाम Characters

Special characters और emojis UTF-8 encoding में एक byte से ज़्यादा लेते हैं। LENGTH total bytes count करता है, जबकि CHAR_LENGTH actual text characters count करता है, इसलिए एक single emoji की LENGTH 4 हो सकती है लेकिन CHAR_LENGTH 1।

उदाहरण: Bytes vs Characters

sql
SELECT LENGTH('a') AS ascii_bytes, CHAR_LENGTH('a') AS ascii_chars;

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

Empty और Null Strings

एक empty string में 0 characters होते हैं, बिल्कुल कोई value न होने से अलग। अगर आप एक NULL value की length check करें, MySQL एक number के बजाय NULL return करता है, क्योंकि आप उस चीज़ की length measure नहीं कर सकते जो वहाँ है ही नहीं।

उदाहरण: Empty and Null Strings

sql
SELECT LENGTH('') AS empty_length, LENGTH(NULL) AS null_length;

Trimming और Length Check करना

Spaces किसी दूसरे character जैसे ही string length में count होते हैं। आप length functions को TRIM जैसे trimming functions के साथ combine करके सिर्फ real characters count कर सकते हैं, accidental leading या trailing whitespace ignore करते हुए।

उदाहरण: Trimming and Checking Length

sql
SELECT LENGTH('  hi  ') AS padded_length, LENGTH(TRIM('  hi  ')) AS trimmed_length;

Performance और Filtering

आप weak passwords या बहुत लंबे inputs filter out करने के लिए अपनी WHERE clauses में string lengths इस्तेमाल कर सकते हैं, जैसे hash होने से पहले ही eight characters से छोटे किसी भी password को reject करना।

उदाहरण: Performance and Filtering

sql
CREATE TABLE users (id INT, password TEXT);
INSERT INTO users VALUES (1, 'abc'), (2, 'longenoughpw');
SELECT * FROM users WHERE LENGTH(password) < 8;
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. accents या emoji वाले text में characters count करने के लिए LENGTH इस्तेमाल करना, जबकि यह bytes count करता है।
  2. यह मान लेना कि एक NULL की LENGTH 0 है, जबकि यह NULL है।
  3. trailing spaces का हिसाब न रखना, जो characters की तरह count होते हैं।
🔒

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.