SQL Server2005 強制轉換類型

在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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章