在SQL SERVER中,cast和convert函数都可用于类型转换,其功能是相同的,只是语法不同.
select CAST(‘143‘ as int) -- 143
select CONVERT(int, ‘143‘) -- 143
select CAST(136.4 as int) -- 136
select CONVERT(int, 136.4) -- 136
select CAST(‘126.4‘ as int)
select CONVERT(int, ‘126.4‘)
-- Conversion failed when converting the varchar value ‘126.4‘ to data type int.
select CAST(‘126.4‘ as decimal) -- 126
select CONVERT(decimal, ‘126.4‘) -- 126
select CAST(‘126.4‘ as decimal(9,2)) -- 126.40
select CONVERT(decimal(9,2), ‘126.4‘) -- 126.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
本文出自 “╰插叙的时光” 博客,请务必保留此出处http://huxianfen.blog.51cto.com/3956001/1361525