코드로 우주평화

SQL: Regex Replace 본문

나는 이렇게 학습한다/Algorithm & SQL

SQL: Regex Replace

daco2020 2022. 7. 15. 18:19

You are given a table named repositories, format as below:

** repositories table schema **

  • project
  • commits
  • contributors
  • address

The table shows project names of major cryptocurrencies, their numbers of commits and contributors and also a random donation address ( not linked in any way :) ).

Your job is to remove all numbers in the address column and replace with '!', then return a table in the following format:

** output table schema **

  • project
  • commits
  • contributors
  • address

 

Solution:

SELECT 
  project, 
  commits, 
  contributors, 
  REGEXP_REPLACE(address ,'[[:digit:]]', '!', 'g') AS address 
FROM 
  repositories

 

Result:

project commits contributors address
Bitcoin 2 7 !!C!Frw!tTJxW!!cShWswpAwbzVbe!!f!y
Ethereum 8 8 !J!W!BVPWFQRb!x!PqHq!gPFE!MjWa!!bG
DASH 6 0 !ADhovPJrn!a!yCiRTggpwZhpaqfCSXJ!Z
Monero 8 8 !!!CVNnEMjemhXw!!R!H!ZKhbiBGC!XSje
Decent 7 1 !BWwyPmuRnW!F!J!VPFXQ!!!KzY!!Zrd!N
Aeternity 7 2 !DS!cLhKyzF!zPNmM!!HWPhhn!!dxLTuUY

 

 

Reference:

 

 

PostgreSQL REGEXP_REPLACE Function By Examples

Summary: in this tutorial, you will learn how to use the PostgreSQL REGEXP_REPLACE() function to replace strings that match a regular expression. The PostgreSQL REGEXP_REPLACE() function replaces substrings that match a POSIX regular expression by a new su

www.postgresqltutorial.com