SQL Server2005 强制转换类型

时间:2014-02-22 15:33:34   收藏:0   阅读:351

在SQL SERVER中,cast和convert函数都可用于类型转换,其功能是相同的,只是语法不同.


select CAST(143as int)   -- 143
select CONVERT(int, 143)  -- 143

select CAST(136.4 as int)   -- 136
select CONVERT(int, 136.4)  -- 136

select CAST(126.4as int)
select CONVERT(int, 126.4)
-- Conversion failed when converting the varchar value ‘126.4‘ to data type int.

select CAST(126.4as decimal)  -- 126
select CONVERT(decimal, 126.4) -- 126


select CAST(126.4as 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

原文:http://huxianfen.blog.51cto.com/3956001/1361525

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!