Pages

Monday, March 30, 2015

TRY_CONVERT FUNCTION

Problem : Do to the explicit conversation using CAST OR CONVERT function , sometimes it's possible that you have passed the value to the conversation is not an appropriate value for that conversation. It will return the conversion failed error.

SELECT CONVERT(INT,'034A')

-- Output

-- Conversion failed when converting the varchar value '034A' to data type int


Solution : To overcome for above issue you can use the TRY_CONVERT function for explicit DataConversation where you can check the datatype also before converting the value.

SELECT  TRY_CONVERT(INT,'012A')



SELECT 'Integer Value Exist' WHERE TRY_CONVERT(INT, '12') IS NOT NULL

-- OutPut

-- Integer Value Exist

No comments:

Post a Comment