In a table containing the CPF column I would like to format it to display the content appropriately. By proper means I mean the format 999.999.999-99 . Is it possible to format this content using regular expression?
SQLFiddle for testing
What you want is possible using the regexp_replace
function:
SELECT REGEXP_REPLACE(cpf,
'([[:digit:]]{3})([[:digit:]]{3})([[:digit:]]{3})([[:digit:]]{2})',
'..-') AS cpf_formatado
FROM cadastro;
The regular expression will divide the digits into 3 groups of 3 and the end into a group of 2. Finally these groups are rearranged with the '..-'
divisions.
Example in sqlfiddle .
It is not good practice to store database data already formatted. It is up to the programmer to format the data in the best way at the time of displaying on the interface, the bank's function is to only provide such data, raw.