- DECLARE @int_digits SMALLINT,@digits SMALLINT,@total_digits SMALLINT,@sqlstr NVARCHAR(100)
- DECLARE @input MONEY
- DECLARE @f FLOAT
- SET @input=234214.4434
- SET @int_digits=floor(log10(@input))+1
- SET @f=@input-floor(@input)
- SELECT @digits=case LEN(CAST(@f AS VARCHAR))-2
- WHEN -1 THEN 2
- WHEN 1 THEN 2
- WHEN 2 THEN 2
- WHEN 3 THEN 3
- ELSE 4
- END
- SET @total_digits=@int_digits+1+@digits
- SET @sqlstr=N'SELECT CAST('+STR(@input,@total_digits,@total_digits)+' AS DECIMAL('+STR(@total_digits)+N','+STR(@digits)+N'))'
- exec sp_executesql @sqlstr
- --该片段来自于http://www.codesnippet.cn/detail/200220132155.html
来源: http://www.codesnippet.cn/detail/200220132155.html