Explained: REPLACE() function in Snowflake?

Are you looking to find how to use the REPLACE() function within the Snowflake cloud data warehouse or maybe you are looking for a solution, how to use the REPLACE function in the Snowflake? If you are looking for any of these problem solutions then you have landed on the correct page. I will also show you what and how to use REPLACE() function. I will explain the REPLACE() function by taking a practical example. So don’t waste time let’s start step by step guide to understanding what is the REPLACE() expression.

What is REPLACE() Function?

Replace() function helps to remove all the occurrences of a specified substring with input string. For example, If substring ‘cc’ can be replaced by ‘bb’ in the given input string ‘abbab’, then the resultant output will be ‘accab’.

How can we use REPLACE() Function?

REPLACE() function is used to remove all the occurrences of a specified substring and optionally replaces them with another string.

What is the syntax of REPLACE() function?

-- Syntax : 

REPLACE ( Subject, Pattern, Replacements )

REPLACE() Argument Details :

SubjectString value
PatternString value
ReplacementString value
Table 1: Replace function in Snowflake Argument List

Subject :

The Subject is the string in which the replacements need to be done. Typically, This might be a column or literal.

Pattern :

This is the substring that should be replaced. Typically, This is literal but, It might be a column or an expression. If you want to use a pattern then you need to use the REGEXP_REPLACE function.

Replacement :

This is the value used as a replacement for the pattern. If this is an empty string then REPLACE function deletes all the occurrences of the patterns.

Return :

The data type returns the string after all the replacements.

Example of Replace Function in Snowflake :

select replace('abcde', 'bcd') from dual;

| ae                      |

create or replace table demo.public.replace_example(subject varchar(10), pattern varchar(10), replacement varchar(10));
| status                                      |
| Table REPLACE_EXAMPLE successfully created. |

insert into demo.public.replace_example values('snowman', 'snow', 'spider'), ('sad face', 'sad', 'happy');

| number of rows inserted |
|                       2 |

select subject, pattern, replacement, replace(subject, pattern, replacement) as new from demo.public.replace_example;

| snowman  | snow    | spider      | spiderman  |
| sad face | sad     | happy       | happy face |

As we can see from the above examples we got the output the pattern column is replaced with replacement column as expected.

Full Example for REPLACE() function in Snowflake?

-- Create a table

create or replace TABLE EMP1 (
	ID NUMBER(38,0),

-- Insert Data

insert into emp(id,name,amount) values 
(4,'Sai Kumar'),
(5,'Naveen Sai'),
(6,'Seshi Kumar'),
(7,'Rajiv Ram'),

-- Table Data

SELECT * from emp;
| ID | NAME        | INFO     |
|  1 | Ram         | NOT-NULL |
|  2 | RAJU        | NOT-NULL |
|  3 | Sameer      | NOT-NULL |
|  4 | Sai Kumar   | NOT-NULL |
|  5 | Naveen Sai  | NOT-NULL |
|  6 | Seshi Kumar | NOT-NULL |
|  7 | Rajiv Ram   | NOT-NULL |
|  8 | NULL        | NOT-NULL |

-- Apply Replace function to the table data.

select id,name,replace(name,'a','f') from emp;
| ID | NAME        | REPLACE(NAME,'A','F') |
|  1 | Ram         | Rfm                   |
|  2 | RAJU        | RAJU                  |
|  3 | Sameer      | Sfmeer                |
|  4 | Sai Kumar   | Sfi Kumfr             |
|  5 | Naveen Sai  | Nfveen Sfi            |
|  6 | Seshi Kumar | Seshi Kumfr           |
|  7 | Rajiv Ram   | Rfjiv Rfm             |
|  8 | NULL        | NULL                  |

When you should use REPLACE() Function in Snowflake?

There are certain use case scenarios when it is recommended to use the REPLACE function within the Snowflake cloud data warehouse which are as follows:

  • If you want to replace any kind of expression or string present in the table data, then we can make use of this function.

Real World Use Case Scenarios for REPLACE Function in Snowflake

  • We got to buy a product, In the place of money we are getting the product. Ultimatly we are replacing money with product.
  • Sometimes we exchanges our clothes with our friends, Indirectly replace function is applied.

REPLACE() Snowflake Official Documentation Link

Final Thoughts

In this article, we have learned about REPLACE() function and uses with the examples explained clearly. I have also covered different scenarios with a practical example that could be possible. I hope the information that was provided is helped in gaining the knowledge.

Please share your comments and suggestions in the comment section below and I will try to answer all your queries as time permits.

Leave a Comment