Cast() 函数用于将数据类型变量或数据从一种数据类型转换为另一种数据类型。

句法

CAST([Expression] AS 数据类型)

要转换表达式的数据类型是目标类型。要转换的表达式的数据类型是源类型。

DECLARE @A varchar(2)    
DECLARE @B varchar(2)

set @A='25a'    
set @B='15'

Select CAST(@A as int) + CAST(@B as int)  as Result 
--'25a' is casted to 25 (string to int)
--'15' is casted to 15 (string to int)

--Result 
 --40

DECLARE @C varchar(2)  = 'a'    

select CAST(@C as int) as Result    
--Result
 --Conversion failed when converting the varchar value 'a' to data type int.

如果失败则引发错误