Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL FORMAT function error

This SQL:

select FORMAT(lNum,'##-###-##-###') 
from  [rpt].[myView] 

Produces the following error:

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

lNum is a varchar(10)

Running SQL Server 2012

like image 478
webdad3 Avatar asked Dec 04 '15 17:12

webdad3


People also ask

What is format function in SQL?

Definition and Usage. The FORMAT() function formats a value with the specified format (and an optional culture in SQL Server 2017). Use the FORMAT() function to format date/time values and number values. For general data type conversions, use CAST() or CONVERT().

How do I change the format in SQL?

You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')

What is the return type of format function in SQL?

FORMAT returns NULL for errors other than a culture that is not valid. For example, NULL is returned if the value specified in format is not valid. The FORMAT function is nondeterministic.


1 Answers

varchar isn't supported as the first argument to FORMAT. The only categories of datatypes supported are Date and Time and Numeric.

You could do

select FORMAT(cast(lNum as numeric),'##-###-##-###') from  [rpt].[myView]  

From levelonehuman's comment : Documentation

like image 80
Vamsi Prabhala Avatar answered Sep 20 '22 08:09

Vamsi Prabhala