Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL TRY_PARSE Function

by suresh

The SQL TRY_PARSE is one of the SQL Conversions Function. This SQL TRY_PARSE function converts the String data to the requested data type and returns the result as an expression. I suggest you to use this SQL TRY_PARSE function to convert the string data to either Date/time, or Number type.

SQL TRY_PARSE Function Syntax

If the SQL Try_Parse function is unable to convert the string into the desired data type, or if we pass the string as non-convertible, then this function will return NULL. The syntax of the SQL Server TRY_PARSE Function is

TRY_PARSE (String_Value AS Data_Type [USING Culture])

-- For example
SELECT TRY_PARSE (String_Column AS Data_Type USING 'en-US') AS [Column1]
FROM [Source]
  • String_Value: Please specify the valid string that you want to convert as the desired data type.
  • Data_Type: Here you have to specify the Data Type to which you want to convert the String_Value
  • Culture: This is an optional parameter. And if you ignore the parameter, then the function will use the language of the current session.

SQL TRY_PARSE Function Example 1

The SQL Server Try_Parse Function is mainly used to convert the string values into date and time, and numeric values. The following query will parse the string to integer and decimal. And also parse the string to date time.

-- SQL TRY_PARSE FUNCTION Example 
DECLARE @str AS VARCHAR(50)
SET @str = '1234'

SELECT TRY_PARSE(@str AS INT) AS Result; 

-- Direct Inputs
SELECT TRY_PARSE('4455' AS DECIMAL(10, 2)) AS Result; 
 
SELECT TRY_PARSE('03/03/2017' AS DATETIME) AS Result;  

SELECT TRY_PARSE('03/03/2017' AS DATETIME2) AS Result; 

SELECT TRY_PARSE('Tutorial Gateway' AS DATETIME USING 'en-US') AS Result;
SQL TRY_PARSE Function 1

Below lines of SQL Server code are used to declare a variable of VARCHAR Data Type. Next, we were assigning the string data ‘1234’.

DECLARE @str AS VARCHAR(50)
SET @str = '1234'

From the below statement, you can see that we are using try parse function for converting the string to an integer. We also assigned new names to the result as ‘Result’ using the ALIAS Column.

SELECT TRY_PARSE(@str AS INT) AS Result;

In the next line, We used the SQL TRY_PARSE function directly on string and converting it to a decimal value

SELECT TRY_PARSE('4455' AS DECIMAL(10, 2)) AS Result;

Next, we are converting the string to Date-Time and DateTime 2 data type. Please refer to SQL Data Types to understand the datatype limitations.

SELECT TRY_PARSE('03/03/2017' AS DATETIME) AS Result;  

SELECT TRY_PARSE('03/03/2017' AS DATETIME2) AS Result;

Lastly, we tried to convert the ‘Tutorial Gateway’ string to date time. As you can see, it is not possible so, this SQL Server Try Parse function is returning NULL as output.

SELECT TRY_PARSE('Tutorial Gateway' AS DATETIME USING 'en-US') AS Result;

Use IIF with SQL TRY_PARSE

In this example, we show you how to use IIF Statement to handle the NULLs returned by the TRY_PARSE. It can be helpful when you are inserting records, and you don’t know whether the column has date and time or not. In this case, you can use this function to check, and if not, we can replace it with the current date and time.

SELECT IIF(
	    TRY_PARSE('Tutorial Gateway' AS DATETIME) IS NULL, 
		       GETDATE(), 
		       'False'
	   ) AS Result;

Here we used the IIF statement along with the IS NULL statement to check whether the result expression is null or not.

SQL TRY_PARSE Function 2

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy