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

REPLACE Function

REPLACE किसी document में find-and-replace इस्तेमाल करने जैसा है, एक text के हर copy को दूसरे से swap करते हुए।
Syntax
sql
REPLACE(string, search_string, replacement_string)

REPLACE Function क्या है?

REPLACE किसी string को एक target substring की हर occurrence के लिए scan करता है और हर एक को एक नई value से swap करता है, एक text editor में find-and-replace जैसा लेकिन सीधे एक query के अंदर चलते हुए।

उदाहरण: What is the REPLACE Function?

sql
SELECT REPLACE('I like cats', 'cats', 'dogs') AS updated_text;

REPLACE में Case Sensitivity

REPLACE characters को exactly compare करता है, इसलिए यह default रूप से case-sensitive है — Cat search करना source string में cat से match नहीं करेगा। inconsistent capitalization वाला data साफ करते समय इसे ध्यान में रखें।

उदाहरण: Case Sensitivity in REPLACE

sql
SELECT REPLACE('I like Cats', 'cats', 'dogs') AS unchanged;
SELECT REPLACE('I like Cats', 'Cats', 'dogs') AS changed;

Substrings हटाना

replacement की तरह एक empty string pass करना किसी और चीज़ से swap करने के बजाय effectively हर match को delete कर देता है। यह किसी phone number column से dashes या parentheses जैसे unwanted symbols strip करने का एक तेज़ तरीका है।

उदाहरण: Removing Substrings

sql
SELECT REPLACE('(555) 123-4567', '-', '') AS no_dashes;

REPLACE इस्तेमाल करके Data Update करना

REPLACE को एक UPDATE statement के साथ pair करना आपको एक pass में किसी column की हर row में एक value fix करने देता है, जो हर record को individually edit करने से कहीं ज़्यादा तेज़ है — किसी mistyped brand name को site-wide correct करने के लिए उपयोगी।

उदाहरण: Updating Data using REPLACE

sql
CREATE TABLE products (id INT, brand TEXT);
INSERT INTO products VALUES (1, 'Acem'), (2, 'Acem Pro');
UPDATE products SET brand = REPLACE(brand, 'Acem', 'Acme');
SELECT * FROM products;

Nested REPLACE Functions

आप एक single expression में कई substitutions apply करने के लिए REPLACE calls को एक-दूसरे के अंदर nest कर सकते हैं, जैसे इसे एक normalized format में store करने से पहले किसी phone number से dashes और spaces दोनों हटाना।

उदाहरण: Nested REPLACE Functions

sql
SELECT REPLACE(REPLACE('555-123 4567', '-', ''), ' ', '') AS cleaned_number;
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. यह उम्मीद करना कि REPLACE case ignore करेगा, जबकि cats की search से Cats replace नहीं होता।
  2. arguments को गलत order में pass करना, जैसे REPLACE(text, new, old)।
  3. बिना WHERE के UPDATE ... SET col = REPLACE(...) चलाना और intended से ज़्यादा rows बदल देना।
🔒

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.