Last active
February 11, 2023 14:55
-
-
Save alsimoes/3ced037130b16ebbd32c to your computer and use it in GitHub Desktop.
SQL code to format CPF/CNPJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
,CASE | |
WHEN LEN([nr_cnpj]) = 10 THEN SUBSTRING([NR_CNPJ],1,2) + '.' + SUBSTRING([NR_CNPJ],3,3) + '.' + SUBSTRING([NR_CNPJ],6,3) + '-' + SUBSTRING([NR_CNPJ],9,2) | |
WHEN LEN([nr_cnpj]) = 11 THEN SUBSTRING([NR_CNPJ],1,3) + '.' + SUBSTRING([NR_CNPJ],4,3) + '.' + SUBSTRING([NR_CNPJ],7,3) + '-' + SUBSTRING([NR_CNPJ],10,2) | |
WHEN LEN([nr_cnpj]) = 12 THEN '00.' + SUBSTRING([NR_CNPJ],1,3) + '.' + SUBSTRING([NR_CNPJ],4,3) + '/' + SUBSTRING([NR_CNPJ],7,4) + '-' + SUBSTRING([NR_CNPJ],11,2) | |
WHEN LEN([nr_cnpj]) = 13 THEN '0' + SUBSTRING([NR_CNPJ],1,1) + '.' + SUBSTRING([NR_CNPJ],2,3) + '.' + SUBSTRING([NR_CNPJ],5,3) + '/' + SUBSTRING([NR_CNPJ],8,4) + '-' + SUBSTRING([NR_CNPJ],12,2) | |
WHEN LEN([nr_cnpj]) = 14 THEN SUBSTRING([NR_CNPJ],1,2) + '.' + SUBSTRING([NR_CNPJ],3,3) + '.' + SUBSTRING([NR_CNPJ],6,3) + '/' + SUBSTRING([NR_CNPJ],9,4) + '-' + SUBSTRING([NR_CNPJ],13,2) | |
ELSE 'Formatação ñ prevista. Contacte o suporte.' | |
END AS [CLIENTE] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment