Format CPF column with regular expression

3

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

    
asked by anonymous 27.07.2015 / 19:57

2 answers

3

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 .

    
28.07.2015 / 01:10
0

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.

    
27.07.2015 / 23:58