Home > SQL Server > SQL SERVER 2012 – Convert an Expression to Specific Data Type using PARSE Conversion Function

SQL SERVER 2012 – Convert an Expression to Specific Data Type using PARSE Conversion Function


Parse : This conversion function is used to compute the expression and return the output to the desired data type. But, this function is in SQL Server 2012 and onwards.

Parse function suitable for data and numeric data and it requires CLR because it is a .NET function. So, as usual there will be some performance impact if we use in large mission critical application. We still have cast and convert functions for type casting.

This function requires a valid data and appropriate convertible data type. Otherwise, parse function raise error.

I think the best feature is this function is language or culture option. We can specify the culture type in the conversion. So, the output reflects the given culture with converted data type.

Usage

PARSE ( string_value AS data_type [ USING culture ] )

string_value : It is nvarchar(4000) storage.

data_type : These are the valid data types

pic1culture : Optional string that identifies the culture in which string_value is formatted. Please refer this blog post for languages in SQL Server.

Example

1. Parsing currency of different region.

SELECT PARSE(‘€345,98’ AS money USING ‘de-DE’) AS Result;

Output

pic12. Parsing date of the given language or locale

SELECT PARSE(’13 August 2013′ AS datetime2 USING ‘en-us’) AS Result;

Output

pic2

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: