Error formatting ZIP with Format() function

Asked

Viewed 32 times

0

When trying to format the data of the CEP variable, SQL Server returns the following message:

Argument data type varchar is invalid for argument 1 of format Function.

Just follow my code:

DECLARE @cep VARCHAR(9) = '23574510';
SELECT FORMAT(@cep,'#####-###') AS [CEP]

Where am I going wrong?

  • 3

    the function FORMAT accepts only numbers and dates as parameter: https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-ver15

  • Thank you Ricardo!

1 answer

1

The function FORMAT accepts only numeric types and date types. You could do so:

DECLARE @cep int = 23574510;
SELECT FORMAT(@cep,'#####-###') AS [CEP]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.