Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 60

MySQL String Functions

String Functions in Reports

String functions combine, normalize, measure and extract text for query output. They should not replace proper validation or a clean data model.

SELECT full_name,
       CONCAT(full_name, ' - ', class_name) AS student_label
FROM students
ORDER BY student_id;
Aarav - X-A Meera - X-A Kabir - X-B Sana - X-B

CONCAT, CONCAT_WS and NULL

SELECT CONCAT('Coding', 'Easily') AS joined,
       CONCAT_WS(' - ', 'Aarav', 'X-A') AS label;
CodingEasily | Aarav - X-A

CONCAT returns NULL if any argument is NULL. CONCAT_WS uses a separator and skips NULL arguments after the separator. Use COALESCE when a visible fallback is required.

Case and Whitespace

SELECT UPPER('Meera') AS upper_name,
       LOWER('SQL Tutorial') AS lower_text,
       TRIM('  SQL  ') AS clean_text,
       LTRIM('  left') AS left_clean,
       RTRIM('right  ') AS right_clean;
MEERA | sql tutorial | SQL | left | right

These transform output. Cleaning stored data requires a reviewed UPDATE and constraints at input. Case conversion depends on character set and collation rules.

CHAR_LENGTH vs LENGTH

SELECT CHAR_LENGTH('Coding Easily') AS characters,
       LENGTH('Coding Easily') AS bytes;
13 | 13

ASCII uses one byte per character here, so results match. Unicode characters can use multiple bytes; use CHAR_LENGTH for user-visible character count and LENGTH for storage bytes.

SUBSTRING, REPLACE and LOCATE

SELECT SUBSTRING('CodingEasily', 1, 6) AS first_word,
       REPLACE('X-A', 'X-', 'Class ') AS class_label,
       LOCATE('Easily', 'CodingEasily') AS start_position;
Coding | Class A | 7

SUBSTRING positions start at 1. REPLACE is case-sensitive for matching. LOCATE returns 0 when the substring is absent.

Equality, ordering and LIKE behavior depend on collation. Avoid applying LOWER or TRIM to every row in a large search merely to force matching. Normalize at input, choose the correct collation and measure with EXPLAIN. If a transformed search is essential, evaluate a generated/functional indexed expression supported by your design.

Never concatenate untrusted text into SQL. String functions format data; prepared statements protect query syntax.

Practice

  1. Create a student label with class.
  2. Display names in uppercase without modifying storage.
  3. Compare CHAR_LENGTH and LENGTH on Hindi text.
  4. Extract the first three characters of each name.
  5. Find the position of “Easily” in CodingEasily.

Quick Summary

  • CONCAT combines; case and trim functions normalize output.
  • CHAR_LENGTH counts characters and LENGTH bytes.
  • SUBSTRING extracts, REPLACE substitutes and LOCATE finds.
  • NULL, Unicode, collation and index use must be considered.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

What is the difference between CHAR_LENGTH and LENGTH?
CHAR_LENGTH returns characters; LENGTH returns bytes. They match for ASCII but can differ for multibyte Unicode text.
What happens when CONCAT receives NULL?
CONCAT returns NULL if any argument is NULL. Use CONCAT_WS or COALESCE when optional parts should be handled.
Does UPPER permanently change stored text?
Not in SELECT. It transforms the result expression only. UPDATE is required to change stored data.
Is SUBSTRING position zero-based?
No. MySQL string positions normally start at 1. A negative start can count from the end.
Can functions on a WHERE column affect indexes?
Yes. Wrapping an indexed column in LOWER, TRIM or another function can prevent normal index range use unless a suitable functional index/design is used.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.