Samy
Samy

Reputation: 485

Set default value in case of error in update SQL statement

Is there a way in Sql Server 2012 to set a default value on a column, for example:

convert(datetime,'1900/1/01', 120)

In case the update statement fails:

update Table1 set [UpdDate] = CONVERT(datetime, [UpdDate0])

For example the update statement encounters strange values (English or Chinese letters) on UpdDate0, so it cannot convert them to datetime and fails.

In those cases, I want to be able to set the default value I mentioned, or even a blank value, anything at all would be better than an error.

Thank you for any suggestions!

Upvotes: 1

Views: 848

Answers (1)

Prashant
Prashant

Reputation: 460

you can use try_convert. Something like given below

SELECT CASE WHEN TRY_CONVERT(float, 'test') IS NULL THEN 'Cast failed' ELSE 'Cast succeeded' END AS Result;

Upvotes: 3

Related Questions