sql server中强制类型转换cast和convert的区别
在sql server中,cast和convert函数都可用于类型转换,其功能是相同的,
只是语法不同.
cast一般更容易使用,convert的优点是可以格式化日期和数值.
select cast('123' as int) -- 123 select convert(int, '123') -- 123 select cast(123.4 as int) -- 123 select convert(int, 123.4) -- 123 select cast('123.4' as int) select convert(int, '123.4') -- conversion failed when converting the varchar value '123.4' to data type int. select cast('123.4' as decimal) -- 123 select convert(decimal, '123.4') -- 123 select cast('123.4' as decimal(9,2)) -- 123.40 select convert(decimal(9,2), '123.4') -- 123.40 declare @num money set @num = 1234.56 select convert(varchar(20), @num, 0) -- 1234.56 select convert(varchar(20), @num, 1) -- 1,234.56 select convert(varchar(20), @num, 2) -- 1234.5600
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!