--- 中文版
set ansi_nulls on
goset quoted_identifier on
gocreate function [dbo].[atoc] (@changemoney money)
returns varchar(100)
as
begin
declare @string1 char(20)
declare @string2 char(30)
declare @string4 varchar(100)
declare @string3 varchar(100) --從原a值中取出的值
declare @i int --迴圈變數
declare @j int --a的值乘以100的字串長度
declare @ch1 varchar(100) --數字的漢語讀法
declare @ch2 varchar(100) --數字位的漢字讀法
declare @zero int --用來計算連續有幾個零
declare @returnvalue varchar(100)
select @returnvalue = ''
select @string1 = '零壹貳叄肆伍陸柒捌玖'
select @string2 = '萬仟佰拾億仟佰拾萬仟佰拾元角分'
select @string4 = cast(@changemoney*100 as decimal(20,0))
select @j=len(cast((@changemoney*100) as decimal(20,0)))
select @string2=right(@string2,@j)
select @i = 1
while @i<= @j begin
select @string3 = substring(@string4,@i,1)
if @string3<>'0' begin
select @ch1 = substring(@string1, cast(@string3 as int) + 1, 1)
select @ch2 = substring(@string2, @i, 1)
select @zero = 0 --表示本位不為零
endelse begin
if (@zero = 0) or (@i = @j - 9) or (@i = @j - 5) or (@i = @j - 1)
select @ch1 = '零'
else
select @ch1 = ''
select @zero = @zero + 1 --表示本位為0
--如果轉換的數值需要擴大,那麼需改動以下表示式 i 的值。
select @ch2 = ''
if @i = @j - 10 begin
select @ch2 = '億'
select @zero = 0
endif @i = @j - 6 begin
select @ch2 = '萬'
select @zero = 0
endif @i = @j - 2 begin
select @ch2 = '元'
select @zero = 0
endif @i = @j
select @ch2 = '整'
end
select @returnvalue = @returnvalue + @ch1 + @ch2
select @i = @i+1
end--最後將多餘的零去掉
if charindex('萬萬',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '萬萬', '萬')
if charindex('仟仟',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '仟仟', '仟')
if charindex('佰佰',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '佰佰', '佰')
if charindex('零元',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '零元', '元')
if charindex('零萬',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '零萬', '萬')
if charindex('零億',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '零億', '億')
if charindex('零整',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '零整', '整')
if charindex('零佰',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '零佰', '零')
if charindex('零仟',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '零仟', '零')
if charindex('元元',@returnvalue) <> 0
select @returnvalue = replace(@returnvalue, '元元', '元')
return @returnvalue
end------ 英文版
set ansi_nulls on
goset quoted_identifier on
go/****** 物件: 使用者定義的函式 dbo.ntoc 指令碼日期: 2005-9-7 9:39:40 ******/
create function [dbo].[atoe] (@n numeric(18,2))
returns varchar(200)
asbegin
declare @exp varchar(200)
set @exp=''
declare @million int,
@thousand int,
@last int,
@snumber varchar(20),
@dot int,
@number int,
@decimal int
set @snumber=rtrim(ltrim(str(@n,20,2)))
set @dot=charindex('.',@snumber)
if @n>0
begin
set @number=cast(left(@snumber,@dot-1) as int)
set @million=@number/1000000
if @million>0
set @exp=@exp+dbo.ntoc1000(@million)+'million '
set @thousand=((@number-@million*1000000)/1000)
if @thousand>0
begin
if @exp<>''
set @exp=@exp+'and '+dbo.ntoc1000(@thousand)+'thousand '
else
set @exp=dbo.ntoc1000(@thousand)+'thousand '
endset @last=(@number%1000)
if @last>0
begin
if @exp=''
set @exp=dbo.ntoc1000(@last)
else
set @exp=@exp+dbo.ntoc1000(@last)
endend
set @decimal=cast(substring(@snumber,@dot+1,100) as int)
if @decimal>0
set @exp=@exp+'and cents'+dbo.ntoc1000(@decimal)
set @exp=@exp+'only.'
return @exp
end
數字轉RMB大寫
public class rmb str2 str2.substring 15 j 取出對應位數的str2的值。如 200.55,j為5所以str2 佰拾元角分 迴圈取出每一位需要轉換的值 for i 0 i j i else else else else else else else if i j...
全形轉半形函式 全形數字轉半形數字
private static char doublebytetohalfangle char c byte bs system.text.encoding.unicode.getbytes cs,0,1 if bs.length 2 return cs 0 private static string...
SQL函式 字元函式
字元函式 length 表示字串的長度 select length join 返回值為 4 select length 張三丰hahaha 返回值為 15 在utf 8中乙個漢字佔三個位元組,在gbk中乙個漢字佔兩個位元組 concat 拼接字串 select concat last name,fi...