The difference between cast and convert forced type conversions in SQL Server
In SQL Server, both the cast and CONVERT functions are available for type conversions, and their functions are the same.
It's just a different syntax.
Cast is generally easier to use, and the advantage of convert is that dates and values can be formatted.
Code SelectCAST(‘123‘AsInt)--123
SelectCONVERT(Int,‘123‘)--123
SelectCAST(123.4AsInt)--123
SelectCONVERT(Int,123.4)--123
SelectCAST(‘123.4‘AsInt)
SelectCONVERT(Int,‘123.4‘)
--Conversion failed when converting the varchar value ' 123.4 ' to data type int.
SelectCAST(‘123.4‘AsDecimal)--123
SelectCONVERT(Decimal,‘123.4‘)--123
SelectCAST(‘123.4‘AsDecimal(9,2))--123.40
SelectCONVERT(Decimal(9,2),‘123.4‘)--123.40
Declare@NumMoney
Set@Num=1234.56
SelectCONVERT(varchar(20),@Num,0)--1234.56
SelectCONVERT(varchar(20), @Num , 1) -- 1,234.56
select convert< Span style= "color: #000000;" > (varchar 20), @Num 2) -- 1234.5600
Forcing type conversions between cast and convert in SQL Server