`
yiyeqinghuasoon
  • 浏览: 644842 次
文章分类
社区版块
存档分类
最新评论

【叶子函数分享四十三】SQL数字转英文函数

 
阅读更多

--晴天兄(qianjin036a)的发帖地址:

--http://topic.csdn.net/u/20080614/12/d26adea8-ac05-4b06-8b8a-f46a4b564e3b.html

-- 数字转英文

-- =============================================

-- Author: qianjin036a

-- Create date:06/14/2008 02:27:17

-- Description:Arabic numerals to English

-- =============================================

go

--创建函数

CREATE FUNCTION Digit2English

(

@arabia decimal(38,17)

)

RETURNS varchar(1000)

AS

BEGIN

declare @atoe table(a int,e varchar(10))

insert into @atoe select 0,'zero' union all select 1,'one'

union all select 2,'two' union all select 3,'three'

union all select 4,'four' union all select 5,'five'

union all select 6,'six' union all select 7,'seven'

union all select 8,'eight' union all select 9,'nine'

declare @integer bigint,@trillion int,@billion int,@million int,@thousand int,@hundred int,@english varchar(1000)

select @integer=@arabia,@english=''

select @trillion=@integer % 1000000000000000/1000000000000,@billion=@integer % 1000000000000/1000000000,

@million=@integer % 1000000000/1000000,@thousand=(@integer % 1000000)/1000,@hundred=(@integer % 1000)

if @trillion>0

set @english=@english + dbo.ThreeDigit(@trillion) + 'trillion '

if @billion>0

set @english=@english + dbo.ThreeDigit(@billion) + 'billion '

if @million>0

set @english=@english + dbo.ThreeDigit(@million) + 'million '

if @thousand>0

set @english=@english + dbo.ThreeDigit(@thousand) + 'thousand '

if @hundred>0

set @english=@english + dbo.ThreeDigit(@hundred)

if @english=''

set @english='zero '

if @arabia-@integer>0.000000000

begin

declare @decimal decimal(18,17)

select @english=@english+'point ',@decimal=@arabia-@integer

while @decimal>0.0

begin

select @english=@english+e+' ' from @atoe where cast(@decimal*10 as int)=a

set @decimal=@decimal*10-cast(@decimal*10 as int)

end

end

return @english

END

GO

-- =============================================

-- Author: qianjin036a

-- Create date: 06/14/2008 02:27:17

-- Description: Three Digit Arabic numerals to English

-- =============================================

CREATE FUNCTION ThreeDigit

(

@integer int

)

RETURNS varchar(100)

WITH EXECUTE AS CALLER

AS

BEGIN

declare @atoe table(a int,e varchar(10))

insert into @atoe select 0,'zero' union all select 1,'one'

union all select 2,'two' union all select 3,'three'

union all select 4,'four' union all select 5,'five'

union all select 6,'six' union all select 7,'seven'

union all select 8,'eight' union all select 9,'nine'

union all select 10,'ten' union all select 11,'eleven'

union all select 12,'twelve' union all select 13,'thirteen'

union all select 14,'fourteen' union all select 15,'fifteen'

union all select 16,'sixteen' union all select 17,'seventeen'

union all select 18,'eighteen' union all select 19,'nineteen'

union all select 20,'twenty' union all select 30,'thirty'

union all select 40,'forty' union all select 50,'fifty'

union all select 60,'sixty' union all select 70,'severty'

union all select 80,'eighty' union all select 90,'ninety'

declare @english varchar(100)

set @english=''

if @integer>99

begin

select @english=e+' hundred ' from @atoe where @integer/100=a

set @integer=@integer % 100

if @integer>0

set @english=@english+'and '

end

if @integer<=20 and @integer>0

select @english=@english+e+' ' from @atoe where @integer=a

if @integer>20

begin

select @english=@english+e+' ' from @atoe where @integer/10*10=a

set @integer=@integer % 10

if @integer>0

select @english=@english+e+' ' from @atoe where @integer=a

end

RETURN @english

END

GO

select dbo.digit2english(123456789987654.321)

union all select dbo.digit2english(120045080045054.8412)

union all select dbo.digit2english(0.0102541)

go

/*

---------------------------------------------------------------------

one hundred and twenty three trillion four hundred and fifty six billion seven hundred and eighty nine million nine hundred and eighty seven thousand six hundred and fifty four point three two one

one hundred and twenty trillion forty five billion eighty million forty five thousand fifty four point eight four one two

zero point zero one zero two five four one

*/

分享到:
评论

相关推荐

    sqlserver (parse name)字符串截取的方法

    代码如下:parsename可以返回四个值 = Object name = Schema name = Database name = Server name select parsename(ip,1) from cs,2,3,4 中的... 您可能感兴趣的文章:SQL截取字符串函数分享SQL 截取字符串应用代码

    sql总结.doc

    作用的四条加上 1.分布式工作。 应用程序和数据库的编码工作可以分别独立进行,而不会相互影响。 缺点:1.如果更改范围大到需要对输入存储过程的参数进行更改,或者要更改由其返回的数据,则您仍需要更新程序集中的...

    叶子asp分页类0.12

    名称: 叶子asp分页类 Name: ShowoPage(vbs class) RCSfile: ReadMe.txt Revision: 0.12.20051114.f Author: Yehe(叶子) Released: 2005-11-14 09:40:13 Descript: ASP分页类,支持access/mssql/mysql/sqlite ...

    详解数据库_MySQL: mysql函数

    四舍五入 向上取整 向下取整 2.字符串函数 length() 字节长度 char_length() 字符长度 ucase() 大写 lcase() 小写 concat(字符,…,字符n) 连接字符串 replace(字符串,旧字符,新字符)字符串替换 截取字符串 ...

    《数据库系统及应用》作业.pdf

    上述三个步骤的全部 SQL 语句,包括:建立数据库和 YGB 表的 SQL、触发器 ygb_safety 定义 SQL、函数 Find_Leader 的代码。 3. 含有截图的 WORD 文件。这些截图至少包括: YGB 表结构截图 YGB 数据内容截图,和公司...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    第三章 Sql查询与函数 一、 SQL概述 SQL(Structured Query Language)结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。同时也是数据库脚本文件的扩展名。  SQL...

    jdbc操作文档,数据库基本操作文档集合

    13.dual表是用来计算的表 可以用来round:四舍五入,trunc:截断小数位,mod取余,ceil:求大于等于某数的最小整数。例:ceil(2.35) 3,floor:求小于等于某数的最大整数。例:floor(2.35) 2。 select round(123.123,要...

    MYSQL无限分类算法文档

    包括了一些必要的SQL语句和高级数据管理的讲解,像邻接表模型、检索整树、检索所有叶子节点、检索单一路径、邻接表模型的局限性、嵌套集合(Nested Set)模型、检索子树的深度、嵌套集合模型中集合函数的应用、删除...

    二十三种设计模式【PDF版】

    下去,浓淡几个叶子,待毛笔的水墨要干枯时,画一下树干,这样,一个活生写意的树就画出来. 我上面这些描述其实都是一种模式,创建模式的人是大师,但是拘泥于模式的人永远是工匠. 再回到传统建筑中,中国的传统建筑是过分...

    简单谈谈MySQL的loose index scan

    众所周知,InnoDB采用IOT(index organization table)即所谓的索引组织表,而叶子节点也就存放了所有的数据,这就意味着,数据总是按照某种顺序存储的。所以问题来了,如果是这样一个语句,执行起来应该是怎么样的...

Global site tag (gtag.js) - Google Analytics