Need help in the sql function

Hi, I have the column which return the string as below:
-AE,BH,BR,CA,CH,CN,GB,HK,IE,KW,MX,NL,OM,QA,SA,SG,US

I need to have the above string converted to:
-AE,-BH,-BR,-CA,-CH,-CN,-GB,-HK,-IE,-KW,-MX,-NL,-OM,-QA,-SA,-SG,-US

Just in case a value other than the first one might already have a dash in front of it, you can do this:


select replace(replace(string, ',-', ','), ',', ',-')
2 Likes

Well, you can use the below function in SQL to get what you are looking for.

SELECT '-' || REPLACE(your_column, ',', ',-') AS modified_column
FROM your_table;

Thanks

original_string = "-AE,BH,BR,CA,CH,CN,GB,HK,IE,KW,MX,NL,OM,QA,SA,SG,US"

Split the original string into a list of country codes

country_codes = original_string.split(',')

Add a hyphen "-" before each country code and join them back into a string

modified_string = ','.join(['-' + code for code in country_codes])

print(modified_string)