SQL 數字轉換英文函式
![SQL 數字轉換英文函式](https://i.iter01.com/images/304cb4d516bdf3b96f0d0d5d9449f8d2292fba9a54aa51d23cc5091c91574e59.gif)
USE [WWERP]
GO
/****** 物件: UserDefinedFunction [dbo].[fun_currencyENG] 指令碼日期: 08/02/2009 15:58:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fun_currencyENG] (@num numeric(15,2))
RETURNS varchar(400)
AS
BEGIN
DECLARE @i int,@hundreds int,@tenth int,@one int
DECLARE @thousand int,@million int,@billion int
DECLARE @numbers varchar(400),@s varchar(15),@result varchar(400)
SET @numbers='ONE TWO THREE FOUR FIVE '
+'SIX SEVEN EIGHT NINE TEN '
+'ELEN TWELEVE THIRTEEN FOURTEEN FIFTEEN '
+'SIXTEEN SEVENTEEN EIGHTEEN NINETEEN '
+'TWENTY THIRTY FORTY FIFTY '
+'SIXTY SEVENTY EIGHTY NINETY '
SET @s=RIGHT('000000000000000'+CAST(@num AS varchar(15)),15)
SET @billion=CAST(SUBSTRING(@s,1,3) AS int)--將12位整數分成4段:十億、百萬、千、百十個
SET @million=CAST(SUBSTRING(@s,4,3) AS int)
SET @thousand=CAST(SUBSTRING(@s,7,3) AS int)
SET @result=''
SET @i=0
WHILE @i<=3
BEGIN
SET @hundreds=CAST(SUBSTRING(@s,@i*3+1,1) AS int)--百位0-9
SET @tenth=CAST(SUBSTRING(@s,@i*3+2,1) AS int)
SET @one=(CASE @tenth WHEN 1 THEN 10 ELSE 0 END)+CAST(SUBSTRING(@s,@i*3+3,1) AS int)--個位0-19
SET @tenth=(CASE WHEN @tenth<=1 THEN 0 ELSE @tenth END)--十位0、2-9
IF (@i=1 and @billion>0 and (@million>0 or @thousand>0 or @hundreds>0)) or
(@i=2 and (@billion>0 or @million>0) and (@thousand>0 or @hundreds>0)) or
(@i=3 and (@billion>0 or @million>0 or @thousand>0) and (@hundreds>0))
SET @result=@result+', '--百位不是0則每段之間加連線符,
IF (@i=3 and (@billion>0 or @million>0 or @thousand>0) and (@hundreds=0 and (@tenth>0 or @one>0)))
SET @result=@result+' and '--百位是0則加連線符AND
IF @hundreds>0
SET @result=@result+RTRIM(SUBSTRING(@numbers,@hundreds*10-9,10))+' HUNDRED'
IF @tenth>=2 and @tenth<=9
BEGIN
IF @hundreds>0
SET @result=@result+' AND '
SET @result=@result+RTRIM(SUBSTRING(@numbers,@tenth*10+171,10))
END
IF @one>=1 and @one<=19
BEGIN
IF @tenth>0
SET @result=@result+'-'
ELSE
IF @hundreds>0
SET @result=@result+' AND '
SET @result=@result+RTRIM(SUBSTRING(@numbers,@one*10-9,10))
END
IF @i=0 and @billion>0
SET @result=@result+' BILLION'
IF @i=1 and @million>0
SET @result=@result+' MILLION'
IF @i=2 and @thousand>0
SET @result=@result+' THOUSAND'
SET @i=@i+1
END
IF SUBSTRING(@s,14,2)<>'00'
BEGIN
SET @result=@result+' POINT '
IF SUBSTRING(@s,14,1)='0'
SET @result=@result+'ZERO'
ELSE
SET @result=@result+RTRIM(SUBSTRING(@numbers,CAST(SUBSTRING(@s,14,1) AS int)*10-9,10))
IF SUBSTRING(@s,15,1)<>'0'
SET @result=@result+' '+RTRIM(SUBSTRING(@numbers,CAST(SUBSTRING(@s,15,1) AS int)*10-9,10))
END
RETURN(@result+' ONLY')
END
/*
declare @kk varchar(50)
set @kk = dbo.fun_currencyEng(9990)
select @kk as Eng
*/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-611139/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 英文的月份轉數字及數字轉英文Python
- sql server 資料型別轉換函式SQLServer資料型別函式
- sql隱式轉換SQL
- 字串與數字轉換函式 | 全方位認識 sys 系統庫字串函式
- 將數值轉換為字串的函式字串函式
- ORACLE SQL函式中文漢字轉拼音首字母OracleSQL函式
- mysql 字串和數字比,字串會隱式轉換為數字0MySql字串
- 8.轉換文字函式函式
- Oracle OCP(05):轉換函式Oracle函式
- JavaScript字串轉換數字JavaScript字串
- 校驗英文和數字正規表示式
- SQL中的替換函式replace()使用SQL函式
- Oracle OCP(03):字元函式、數字函式和日期函式Oracle字元函式
- 中文數字阿拉伯數字相互轉換
- 阿拉伯-漢字-數字轉換
- PHP 將數字轉換為漢字PHP
- string與數字相互轉換
- java將IP地址轉換為數字以及逆向轉換Java
- GaussDB: db2->gaussdb 函式轉換DB2函式
- 【SQL】19 SQL函式SQL函式
- sql函式SQL函式
- Python數字轉換中文大寫Python
- Thymeleaf將字串轉換為數字字串
- 將算數表示式轉換成字尾表示式並計算結果
- SQL-函式 - 聚合函式SQL函式
- python函式每日一講 - float函式型別轉換詳解Python函式型別
- 【Go】IP地址轉換:數字與字串之間高效轉換Go字串
- 函式的關鍵字引數函式
- 函式匹配和實參型別轉換函式型別
- C++型別轉換建構函式C++型別函式
- PHP 阿拉伯數字和中文數字的相互轉換PHP
- Python 轉換金額數字大寫為數字小寫Python
- 建構函式定義的隱式型別轉換函式型別
- C# 顯式轉換關鍵字 explicitC#
- Sql 中的 left 函式、right 函式SQL函式
- T-SQL——函式——字串操作函式SQL函式字串
- 將你的Gradle依賴轉換為函式Gradle函式
- js顯式轉換和隱式轉換JS
- leedcode-數字轉換為十六進位制數