2

I have a query like below.

SELECT FORMAT (Submitted_time,'dd-MM-yyyy h:mm tt')
FROM header

And I get the expected output on some of the latest SQL Server as :

27-07-2020 9:15 AM

But when executing same query on some other SQL Server machine, it throws an exception

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

How can I solve this?

3
  • 5
    Update the older servers to be on a supported version. FORMAT was introduced a few versions ago; all currently supported versions have it. Commented Oct 7, 2020 at 7:38
  • 2
    FORMAT isnt a particularly performant function though, you're better off with CONVERT and style codes. Commented Oct 7, 2020 at 7:42
  • 1
    Just avoid running FORMAT on millions of rows and you won't notice the difference. If you are formatting something for human consumption likely the number of rows will be small anyway Commented Oct 7, 2020 at 7:53

3 Answers 3

6

The FORMAT function was introduced with SQL Server 2012 and as pointed in the docs it is available in all supported versions.

You are either running it in older version or the compatibility of the database is set to earlier.

Sign up to request clarification or add additional context in comments.

Comments

1

Not all SQL Server versions FORMAT function work You can use convert as below:

DECLARE @Submitted_time datetime 

SET @Submitted_time= GETDATE()

SELECT CONVERT(VARCHAR(30),@Submitted_time , 121)

Output

2020-10-07 09:11:07.923

1 Comment

Works for ancient SQL Server versions like 2008. Magic number 121 = yyyy-mm-dd hh:mi:ss.mmm (24-hour) See others here: learn.microsoft.com/en-us/sql/t-sql/functions/…
0

You clearly have a pre-2012 version of SQL Server or a compatibility level set to an earlier version. You may be able to construct the string that you want using:

select replace(convert(varchar(255), getdate(), 105) + ' ' + right(convert(varchar(255), getdate()), 7), '  ', ' ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.