Saturday, July 23, 2011

SQL FORMAT() Function


The FORMAT() Function

The FORMAT() function is used to format how a field is to be displayed.

SQL FORMAT() Syntax

SELECT FORMAT(column_name,format) FROM table_name

ParameterDescription
column_nameRequired. The field to be formatted.
formatRequired. Specifies the format.


SQL FORMAT() Example

We have the following "Products" table:
Prod_IdProductNameUnitUnitPrice
1Jarlsberg1000 g10.45
2Mascarpone1000 g32.56
3Gorgonzola1000 g15.67
Now we want to display the products and prices per today's date (with today's date displayed in the following format "YYYY-MM-DD").
We use the following SELECT statement:
SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate
FROM Products
The result-set will look like this:
ProductNameUnitPricePerDate
Jarlsberg10.452008-10-07
Mascarpone32.562008-10-07
Gorgonzola15.672008-10-07

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Deep's | Bloggerized by Deep - Deep's Templates | ElearSQL-Server