REPLACE Function
In this page:
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?
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
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
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
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
SELECT REPLACE(REPLACE('555-123 4567', '-', ''), ' ', '') AS cleaned_number;
- यह उम्मीद करना कि
REPLACEcase ignore करेगा, जबकिcatsकी search सेCatsreplace नहीं होता। - arguments को गलत order में pass करना, जैसे
REPLACE(text, new, old)। - बिना
WHEREकेUPDATE ... SET col = REPLACE(...)चलाना और intended से ज़्यादा rows बदल देना।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: