在指定字符中找到对应的英文大写信息
CREATE function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
return substring(@str,@start,@location-@start)
end
英文大写转换SQL过程,其中会调用上面的过程。
if(Exists(select * from SysObjects where [name]='fn_GetEnglishMoney'))
drop function fn_GetEnglishMoney
go
Create Function fn_GetEnglishMoney(@num Numeric(18,2))
returns nvarchar(500)
as begin
Declare @arr1 nvarchar(1000),@arr2 nvarchar(1000),
@arr3 nvarchar(1000),@arr4 nvarchar(1000)
Select @arr1 = ' ,THOUSAND,MILLION,BILLION',
@arr2 = 'ZERO,TEN,TWENTY,THIRTY,FORTY,FIFTY,SIXTY,SEVENTY,EIGHTY,NINETY',
@arr3 = 'ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE',
@arr4 ='TEN,ELEVEN,TWELVE,THIRTEEN,FOURTEEN,FIFTEEN,SIXTEEN,SEVENTEEN,EIGHTEEN,NINETEEN'
Declare @Inum nvarchar(100),@DNum nvarchar(10)
--分解数值
set @Inum = Convert(nvarchar(100),floor(@Num))
set @DNum = Right(Convert(nvarchar(100),floor(@Num*100)),2)
--return @INum+' '+@DNum
--整数转换
Declare @Len int --整数长度
Declare @i int ,@j int --循环数
Declare @Cols int --循环次数
Declare @Start int --开始数值
Declare @strR nvarchar(500) --中间返回值
Declare @strReturn nvarchar(500) --返回值
Declare @Num3 nvarchar(500) --过渡运算值
Declare @Col int
set @Len = len(@Inum)
set @Cols = ceiling(cast(@len as float)/3)
set @Start=@Len-@Cols*3
set @i=@start
set @strReturn =''
set @j=0
while(@i<@len)
begin
set @strR =''
--记录过渡运算值
if(@i>=0) begin
if(@i+3<@Len)
Set @Num3 = substring(@INum,@i+1,3)
else
set @Num3= substring(@INum,@i+1,@Len-@i)
end
else
set @Num3 = substring(@INum,1,@I+3)
--取值完毕
--是否有百位数,如果有则记下
if(Len(@Num3)=3 and @Num3<>'000') begin
if(substring(@Num3,1,1)<>'0')
set @StrR=@StrR+ dbo.Get_StrArrayStrOfIndex(@arr3,',',Convert(int,substring(@Num3,1,1))+1)+' HUNDRED ' --arr3中取数
if(substring(@Num3,2,2)<>'00')
set @StrR =@StrR+' AND'
Set @Num3= substring(@num3,2,Len(@Num3)-1)
end
--确认十位数
if(len(@num3)=2) begin
if(Substring(@Num3,1,1)='0')
Set @Num3= substring(@num3,2,Len(@Num3)-1)
else if(substring(@Num3,1,1)='1')
set @strR = @strR+' ' + dbo.Get_StrArrayStrOfIndex(@arr4,',',Convert(int,substring(@Num3,2,1))+1) --arr4中取数
else begin
set @strR=@strR+' '+ dbo.Get_StrArrayStrOfIndex(@arr2,',',Convert(int,substring(@Num3,1,1))+1) --arr2中取数
if(substring(@Num3,2,1)<>'0')
Set @StrR=@StrR+'-'
Set @Num3= substring(@num3,2,Len(@Num3)-1)
end
end
--确认个位数
if(Len(@Num3)=1 and substring(@Num3,1,1)<>'0') begin
Set @StrR=@StrR+' '+dbo.Get_StrArrayStrOfIndex(@arr3,',',Convert(int,substring(@Num3,1,1))+1) --arr3中取数
end
Set @StrR = @StrR+' '+dbo.Get_StrArrayStrOfIndex(@arr1,',',@Cols-@j)+','
Set @j=@j+1
set @strReturn=@strReturn+isnull(@StrR,'')
set @i=@i+3
end
if(@Dnum<>'00') begin
Set @StrReturn=@StrReturn+' POINT '+
dbo.Get_StrArrayStrOfIndex(@arr2,',',Convert(int,substring(@Dnum,1,1))+1)
if(Substring(@dnum,2,1)<>'0')
Set @StrReturn=@StrReturn+'-'
Set @StrReturn=@StrReturn+
dbo.Get_StrArrayStrOfIndex(@arr3,',',Convert(int,substring(@Dnum,1,1))+1)
end
return @strReturn+' ONLY'
end
go
Select dbo.fn_GetEnglishMoney(1812525124.36)