Sunday, April 1, 2012

FORMAT ( Transact-SQL)

 

Brief Description:      Returns a value formatted with the specified format and optional culture

Syntax:                       FORMAT ( value, format [, culture ] )

Availability:               All Versions of SQL Server 2012

Details:           Use the FORMAT function for locale-aware formatting of date/time and number values as strings. For general type conversions, continue to use CAST or CONVERT.

In the case of errors other than a culture that is not valid, such as a format that is not valid, FORMAT returns a null.

FORMAT relies on the presence of .the .NET Framework Common Language Runtime (CLR).

This function will not be remoted since it depends on the presence of the CLR. Remoting a function that requires the CLR would cause an error on the remote server.

Acceptable data types for the value argument are listed in the following table, together with their .NET Framework mapping information:

Category

Type

.NET type

Numeric

bigint

Int64

Numeric

int

Int32

Numeric

smallint

Int16

Numeric

tinyint

Byte

Numeric

decimal

SqlDecimal

Numeric

numeric

SqlDecimal

Numeric

float

Double

Numeric

real

Single

Numeric

smallmoney

Decimal

Numeric

money

Decimal

Date and Time

date

DateTime

Date and Time

time

TimeSpan

Date and Time

datetime

DateTime

Date and Time

smalldatetime

DateTime

Date and Time

datetime2

DateTime

Date and Time

datetimeoffset

DateTimeOffset

 

Examples:

1.   

 

declare @d datetime = getdate();

select format( @d, 'dd/MM/yyyy', 'en-US' ) as fomatteddate

 

output

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

fomatteddate

01/04/2012

 

  2.

declare @a int = 1234

select format(@a, '0000000000') AS foramt_a

go

 

output

-----------

foramt_a

0000001234

 

 

Pros:  

 

·         Formatting Numeric values with prefix zeros

·         Formatting Dates on different cultures

4 comments:

Anonymous said...

I ωas able tο find good adviсe from youг
artiсlеs.
Also see my web page :: increasing youtube views

Anonymous said...

Whoa! Many thanks! It's my job to want to compose on my blog site or something that is. Am i allowed to have a fragment of the posting for you to my site?
Take a look at my blog post : 17650

Anonymous said...

It’s been recently a short time since I seen the web page.

Just wanted to prevent by way of and desire anyone, and your
own property, quite a content vacation!
Stop by my web blog ; Genital Wart Remover

Anonymous said...

Me personally in addition to my friend have been fighting a
fantastic situation such as this! At this moment we all believe i seemed to be suitable.
hehe! Thanks for the tips any person write-up.
My web page ; Genital Warts Cures