Today, I have provided an article showing how to utilize the new data type conversion functions in SQL Server 2012. These are the three functions:

Parse conversion Function

Try_Parse conversion Function

Try_Convert conversion Function

Let's take a look at a practical example. The example is developed in SQL Server 2012.

Parse conversion Function

This function is used to convert a string to Numeric and Date and Time formats. It will raise an error if translation isn't possible. You may still use CAST or CONVERT for general conversions. It depends on the presence of the CLR.

Syntax

To demonstrate this new conversion function the following defines the syntax:

PARSE ( string_value AS data_type [ USING culture ] )

The Parse Function contains three parameters. The Culture part of the function is optional.

string_value - String value to parse into the Numeric and Date and Time format.

data_type - Returns data type, numeric or datetime type.

culture - Culture part of the function is optional. A language (English, Japanese, Spanish, Danish, French etc.) to be used by SQL Server to interpret data. A culture can be specified if needed; otherwise, the culture of the current session is used. Culture can be any of the .NET supported cultures, not being limited to those supported by SQL Server.

For Example

In this example we see the parse function with Cast and Convert functions. Execute the following to convert a string value to datetime using CAST, CONVERT and PARSE functions:

This is similar to the convert function except it returns null when the conversion fails. If the conversion cannot be completed because the data type of the expression is not allowed to be explicitly converted to the specified data type, an error will be thrown.