declare @input varchar(255) = 'abc'
select * from table where id = CAST(@input as int)
Can I do this so that the cast will fail silently, or default to some user-provided (or system default) value?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From SQL Server 2012 and up you can use
try_convertandtry_parsefunctions.Until then you can use
You may need to tweak the test a bit (e.g. it disallows negative integers and allows positive ones bigger than the maximum int) but if you find a suitable test (e.g. the one here) and use a
CASEstatement you should avoid any casting errors as order of evaluation forCASEis mostly guaranteed.