Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace char at even places in a string

Input String:

522#4#27571#0#46#2#900#1
909#1#4#0#6#2
902#1#121#2#34562#0
902#1#15#2#14#0
1659224789#3#1659224869#2#1659225532#3#1659225592#2
1659225562#1#1659225592#2

I want to replace or select every # which has occurred at even places. when replacing it with ‘,’ the output should be like:

522#4,27571#0,46#2,900#1
909#1,4#0,6#2
902#1,121#2,34562#0
902#1,15#2,14#0
1659224789#3,1659224869#2,1659225532#3,1659225592#2
1659225562#1,1659225592#2

please provide solution using aws redshift

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I have an answer with MySQL and Oracle SQL. U can use this Regexp_Replace with backreferences.

SQL>

 SELECT WORD,REGEXP_REPLACE (WORD,'([#])([0-9])([#])','\1\2,')
      AS RESULT
 FROM 
     (
      SELECT '522#4#27571#0#46#2#900#1' AS WORD FROM DUAL UNION ALL
      SELECT '902#1#121#2#34562#0' AS WORD FROM DUAL UNION ALL
      SELECT '902#1#15#2#14#0' AS WORD FROM DUAL UNION ALL
      SELECT '1659224789#3#1659224869#2#1659225532#3#1659225592#2' AS WORD FROM DUAL
     )

RESULT

522#4,27571#0,46#2,900#1

902#1,121#2,34562#0

902#1,15#2,14#0

1659224789#3,1659224869#2,1659225532#3,1659225592#2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading