ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

SQL:СÊýλ±£Áô£¨Èç¹û±£ÁôµÄÊýΪ0£¬×Ô¶¯²éÕÒÏÂһ룩(Ò»)
2014-11-24 02:59:36 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:5´Î
Tags£ºSQL: Êýλ ±£Áô Èç¹û ×Ô¶¯ ²éÕÒ Ò»Î»

SQL:СÊýλ±£Áô£¨Èç¹û±£ÁôµÄÊýΪ0£¬×Ô¶¯²éÕÒÏÂһ룩
[sql] www.2cto.com
if OBJECT_ID('GetDecimal') is not null
drop function GetDecimal
go
create function [dbo].[GetDecimal_D](
@value varchar(20), --´¦ÀíµÄÖµ
@decimalPlace int --СÊýλ
) returns varchar(20)
as
/***************************************
--¹¦ÄÜ£ºÐ¡Êýλ±£Áô£¨Èç¹û±£ÁôµÄÊýΪ0£¬×Ô¶¯²éÕÒÏÂһ룩
--´´½¨ÈË:zhujt
--´´½¨ÈÕÆÚ£º2012-11-28 14:23:34
***************************************/
begin
declare @result varchar(20) --ÏÔʾ½á¹û
if @value is not null
begin
--´¦ÀíµÄÖµº¬ÓÐ'L'
if charindex('L',@value)>0
set @result=cast(convert(decimal(18,8),replace(@value,'L',''))/2 as varchar)
--´¦ÀíµÄÖµº¬ÓÐ'e'
else if charindex('e',@value)>0 or charindex('E',@value)>0
set @result=cast(convert(decimal(18,8),convert(float,@value)) as varchar)
else set @result=@value
declare @index int, --СÊýµãλÖÃ
@integerPart varchar(10), --ÕûÊý²¿·Ö
@decimalPart varchar(10), --СÊý²¿·Ö
@decimalPartC varchar(10),--СÊý²¿·Ö
@current int=0, --µ±Ç°Î»ÖÃ
@next int, --ÏÂһλµÄÖµ
@j int=1, --Ñ­»·±äÁ¿
@power decimal(18,10)=1, --ƽ·½
@savePard decimal(18,10), --±£Áô²¿·Ö
@flag int=0
--СÊýµãλÖÃ
set @index=charindex('.',@result)
--´¦ÀíµÄÖµ´øÓÐСÊý
if @index>0
begin
--»ñÈ¡ÕûÊý²¿·Ö
set @integerPart=substring(@result,1,@index-1)
--»ñȡСÊý²¿·Ö
set @decimalPart=substring(@result,@index+1,len(@result)-@index)
--»ñÈ¡±£ÁôСÊýλÊýÖµ
set @current=substring(@decimalPart,@decimalPlace,1)
--»ñÈ¡ÏÂһλµÄÖµ
set @next=substring(@decimalPart,@decimalPlace+1,1)
--Èç¹û´¦ÀíÖµÊÇСÓÚ1µÄÖµ
if @integerPart='0'
begin
if @next>=5 --µ±Ç°Î»ÖôóÓÚ0
begin
while @j<=@decimalPlace
begin
set @power=@power*0.1
set @j+=1
end
set @savePard='0.'+substring(@decimalPart,1,@decimalPlace)
set @result=@savePard+@power
end
else
begin
set @decimalPartC=substring(@decimalPart,1,@decimalPlace)
if @current>0 --Èç¹ûµ±Ç°Öµ´óÓÚ0
begin
set @savePard='0.'+substring(@decimalPart,1,@decimalPlace)
set @result=@savePard
end
else if @current=0 and @decimalPartC=0 --Èç¹ûµ±Ç°ÖµµÈÓÚ0
begin
while @j<=LEN(@decimalPart)
begin
set @power=@power*0.1
if @j>=@decimalPlace+1
begin
set @current=@next --µ±Ç°Î»ÖÃ
set @next=substring(@decimalPart,@j,1) --ÏÂһλÖÃ
Ê×Ò³ ÉÏÒ»Ò³ 1 2 ÏÂÒ»Ò³ βҳ 1/2/2
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºSQL: Date Utility ÏÂһƪ£ºSql:È¥³ý×Ö·û´®ÖеÄÏàͬµÄ×Ö·û´®..

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Linuxϵͳ¼ò½é (2025-12-25 21:55:25)
¡¤Linux°²×°MySQL¹ý³Ì (2025-12-25 21:55:22)
¡¤Linuxϵͳ°²×°½Ì³Ì£¨ (2025-12-25 21:55:20)
¡¤HTTP Åc HTTPS µÄ²î„ (2025-12-25 21:19:45)
¡¤ÍøÕ¾°²È«±ØÐ޿ΣºÍ¼ (2025-12-25 21:19:42)