DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS  SQL 2012 PROGRAMMING  DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to use the FORMAT function?

The following Microsoft SQL Server T-SQL sample script demonstrates the the usage of the FORMAT function which is new to SQL Server 2012:

-- US currency formatting

SELECT FORMAT(7771.22,'c','en-us');

-- $1,777.22

 

-- UK currency formatting

SELECT FORMAT(7771.22,'c','en-gb');

-- £1,777.22

 

-- Datetime formatting for US culture

SELECT FORMAT( getdate(), 'yyyy/MM/dd hh:mm:ss tt', 'en-US' ) -- 2016/06/03 09:56:44 AM

SELECT FORMAT( getdate(), 'MMM dd, yyyy hh:mm:ss tt','en-US') -- Jun 03, 2016 09:57:45 AM

 

-- Date formatting

SELECT FORMAT ( getdate(),'y')   -- July, 2016

SELECT FORMAT ( getdate(),'M')   -- July 17

SELECT FORMAT ( getdate(),'d')   -- 7/17/2016

 

-- Percent formatting

SELECT      ProductNumber,

            ListPrice = FORMAT(ListPrice, 'c', 'en-US'),

            Markup = FORMAT(ListPrice / StandardCost, 'p','en-us')

FROM AdventureWorks2012.Production.Product

WHERE ListPrice > 0.0

/*    ProductNumber           ListPrice         Markup

      SA-M198                 $133.34           135.00 %

      SA-M237                 $147.14           135.00 %

      SA-M687                 $196.92           134.99 %

      SA-R127                 $133.34           135.00 %

      SA-R430                 $147.14           135.00 %

      SA-R522                 $196.92           134.99 %  ....*/

 

------------

 

Related articles:

FORMAT (Transact-SQL)

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE