SQL SERVER – 2012 Functions – FORMAT() and CONCAT() – An Interesting Usage

Before continuing this blog post I would like to bring your attention to two of my earlier blog post where I have written in depth about FORMAT and CONCAT function.

Read the above two blog posts if you are interested in learning about the function in depth. Now recently I had need where I have to demonstrate a string on screen like as following:

Current Time is Sunday July 16, 2012

This was indeed not difficult for me to do in SQL Server 2012 as I wrote following comment very quickly.

SELECT 'Current Time is '+FORMAT (GETDATE(), N'dddd MMMM dd, yyyy', 'en-US') AS CurrentTimeString;

If you are using an earlier version than SQL Server 2012 above T-SQL script will not work and will give you an error.

However, while I was writing this I wondered if there is any other way to include the string along with the format. I tried various alteration and they are listed here. One of the interesting ones which came to my mind was about using the CONCAT function.

SELECT CONCAT('Current Time is ', FORMAT (GETDATE(), N'dddd MMMM dd, yyyy', 'en-US')) AS CurrentTimeString;

While I was trying to create a desired string, I came up with following T-SQL script. I can contact string without using CONCAT function and only with the format string.

SELECT FORMAT(GETDATE(), N'"Current Time is "dddd MMMM dd, yyyy', 'en-US') AS CurrentTimeString;

It is important to use double quote around the string in the FORMAT function, or it will give unexpected answer.

I have said it before many times but let me say it again – SQL Server have never stopped to amaze me.

Watch a quick video relevent to this subject:
[youtube=http://www.youtube.com/watch?v=HbbRpg-tHz4]

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Server Management Studio
Previous Post
SQL SERVER – Find Column Used in Stored Procedure – Search Stored Procedure for Column Name
Next Post
SQLAuthority Guest Post – Lessons from Life – Practice Let Go – Srini Chandra (Author of 3 Lives, in search of bliss)

Related Posts

8 Comments. Leave new

  • Heri Hartawan
    July 17, 2012 4:00 am

    I run the script on SQL Server 2008, and I got this message “Msg 195, Level 15, State 10, Line 3
    ‘FORMAT’ is not a recognized built-in function name.”.

    Reply
    • This one-liner may work better for you:
      select ‘Current Time is ‘ + datename(dw, getdate()) + ‘ ‘ + datename(m, getdate()) + substring(convert(char(12), getdate(), 107), 4, 9)

      Reply
    • @Heri, here’s a SELECT which may work better for you:
      select ‘Current Time is ‘ + datename(dw, getdate()) + ‘ ‘ + datename(m, getdate()) + substring(convert(char(12), getdate(), 107), 4, 9)

      Reply
  • Nice blog

    Reply
  • long overdue and needed functions. You’re always a useful resource!

    Reply
    • Sorry to necro a 10 year old post but I strongly recommend NOT using FORMAT for even small numbers of rows Even for things that a relatively complicated to do using CONVERT and other native functions, FORMAT is 20 times slower in duration and CPU.

      For 1:1 matches with native CONVERT functionality, FORMAT is usually about 50 times slower in duration and CPU.

      Reply
  • A bit nit-picky, but I believe you meant “Current Date”, not “Time” – Unless I’m missing something. Thanks for the format tip!

    Reply

Leave a Reply