Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FORMAT function not working in sql server 2008 R2

DECLARE @d DATETIME = '01/01/2011';
SELECT FORMAT ( @d, 'd', 'en-US' ) AS US_Result;

I am using above code in SQL Server 2008 R2 but it is encountering an error:

'FORMAT' is not a recognized built-in function name.

How can I use FORMAT function?

like image 283
RAKESH HOLKAR Avatar asked Jul 16 '12 06:07

RAKESH HOLKAR


People also ask

How do I change the format of a SQL query?

SQL Server FORMAT() Function 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 format a SQL file?

In a SQL document, right-click the SQL script, click Active Format Profile, and select the required format. 2. To apply the selected format profile, select the SQL document or the fragment of the code and press Ctrl+K, then Ctrl+F. In addition, you can create a custom format profile based on the available profiles.

Can you format numbers in SQL?

MySQL FORMAT() FunctionThe FORMAT() function formats a number to a format like "#,###,###. ##", rounded to a specified number of decimal places, then it returns the result as a string.

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

FORMAT function is available from version 2012 onwards. In earlier versions, use this:

DECLARE @d DATETIME = '01/01/2011'; 
SELECT replace(replace(' '+convert(varchar(10),@d,101),' 0',''),'/0','/')

However, formatting is the job of the front end application.

like image 108
Madhivanan Avatar answered Oct 13 '22 00:10

Madhivanan