Search code examples
sqlregexhiveregexp-replace

regexp_replace function in hive to format SSN


Can anyone please help? I want to replace SSN with dash for the given string using regexp_replace in Hive SQL

I am trying with below query but get the result as 1-2-3

select regexp_replace("123790634", '([0-9]{3})([0-9]{2})([0-9]{4})', '\\1-\\2-\\3');

My output should look like 123-79-0634


Solution

  • You can prefer using the dollar prefixed format instead

    select regexp_replace('123790634', '(.{3})(.{2})(.{4})','$1-$2-$3')