String Function: TRY_PARSE()

Try_Parse()

Try_Parse() is the new built in function introduced in sql server 2012.
It is the alternative of Parse() function.
More flexible when converting String to data types.
Return NULL if fails.

 Note: TRY_PARSE function is not a native SQL SERVER function, it is a .NET CLR dependent function. So it will have the performance overhead and also requires the presence of .NET CLR on the database Server. Continue to use the existing CAST and CONVERT functions wherever it is possible.

Example:
-- TRY_PARSE String to INT
SELECT TRY_PARSE('1000' AS INT) AS 'String to INT'
-- TRY_PARSE String to NUMERIC
SELECT TRY_PARSE('1000.06' AS NUMERIC(8,2))
           AS 'String to NUMERIC'
-- TRY_PARSE String to DATETIME
SELECT TRY_PARSE('05-18-2013' AS DATETIME)
           AS 'String to DATETIME'

OutPut:
String to INT
1000

String to NUMERIC
1000.06

String to DATETIME
2013-05-18 00:00:00.000

More Info:

http://sqlhints.com/2013/06/08/try_parse-sql-server-2012-built-in-conversion-function/ 

See Also:
Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment