palraj001
Привет..
Сначала создайте эту таблицу,
CREATE TABLE [dbo].[M_Words](
[Code] [int] NULL,
[WNumber] [int] NULL,
[Wwords] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
затем
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(1,0,'Zero')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(2,1,'One')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(3,2,'Two')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(4,3,'Three')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(5,4,'Four')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(6,5,'Five')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(7,6,'Six')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(8,7,'Seven')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(9,8,'Eight')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(10,9,'Nine')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(11,10,'Ten')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(12,11,'Eleven')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(13,12,'Twelve')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(14,13,'Thirteen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(15,14,'Fourteen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(16,15,'Fifteen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(17,16,'Sixteen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(18,17,'Seventeen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(19,18,'Eighteen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(20,19,'Nineteen')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(21,20,'Twenty')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(22,30,'Thirty')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(23,40,'Forty')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(24,50,'Fifty')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(25,60,'Sixty')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(26,70,'Seventy')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(27,80,'Eighty')
INSERT INTO [M_Words] ([Code],[WNumber],[Wwords])VALUES(28,90,'Ninety')
затем напишите эту функцию
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create function [dbo].[usp_fnNumToWords](@Number Numeric(18,2),@CPaise Char(1))
returns varchar(5000)
BEGIN
Declare @StrNumber varchar(10), @SLacs char(2), @SThou char(2), @SHun char(2)
Declare @STenUnt char(2), @STen char(2), @SUnt char(2), @SDecimal char(2)
Declare @ILacs Int, @IThou Int, @IHun Int, @ITenUnt Int, @ITen Int, @IUnt Int, @IDecimal Int
Declare @SNumToWords varchar(100), @Wwords varchar(10)
Select @StrNumber = Replicate('0',10-Len(LTrim(RTrim(convert(varchar,@Number))))) + LTrim(RTrim(Convert(varchar,@Number)))
--Print @StrNumber
--Print Len(@StrNumber)
Select @SNumToWords = ''
--Print Len(LTrim(RTrim(convert(varchar,@Number))))
If Len(LTrim(RTrim(convert(varchar,@Number)))) > 4
Begin
--Print Len(@StrNumber)
Select @SLacs = Substring(@StrNumber,1,2)
--Print @SLacs
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0
Begin
Select @STen = Substring(@StrNumber,1,1)
Select @SUnt = Substring(@StrNumber,2,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,1,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @ITen
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
--Select @Wwords = ''
--Select @Wwords = Wwords From M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + ' Lacs'
End
Select @SThou = Substring(@StrNumber,3,2)
--Print @SThou
Select @IThou = Convert(int,@SThou)
If @IThou > 0
Begin
Select @STen = Substring(@StrNumber,3,1)
Select @SUnt = Substring(@StrNumber,4,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,3,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @ITen
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
--Select @Wwords = ''
--Select @Wwords = Wwords From M_Words Where WNumber = @IThou
--Print @Wwords
--Select @SNumToWords = @SNumToWords + @Wwords + ' Thousand '
Select @SNumToWords = @SNumToWords + ' Thousand '
End
Select @SHun = Substring(@StrNumber,5,1)
--Print @SHun
Select @IHun = Convert(int,@SHun)
If @IHun > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @IHun
--Print @Wwords
Select @SNumToWords = @SNumToWords + @Wwords + ' Hundred'
End
Select @STenUnt = Substring(@StrNumber,6,2)
---Print @STenUnt
Select @ITenUnt = Convert(int,@STenUnt)
If @ITenUnt > 0
Begin
Select @STen = Substring(@StrNumber,6,1)
Select @SUnt = Substring(@StrNumber,7,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,6,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @ITen
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
End
Select @SNumToWords = @SNumToWords + Space(1) + 'Rupees' --Only/-
End
Else
Begin
--Print Len(@StrNumber)
--Print LTrim(RTrim(convert(varchar,@Number)))
Select @SLacs = Substring(LTrim(RTrim(convert(varchar,@Number))),1,1)
--Print @SLacs
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0 and @ILacs <> 1
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords + Space(1) + 'Rupees'
End
Else
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + @Wwords + Space(1) + 'Rupee'
End
End
If @CPaise = 'Y'
Begin
Select @SDecimal = Substring(@StrNumber,9,2)
Select @IDecimal = Convert(int,@SDecimal)
If @IDecimal > 0
Begin
Select @SNumToWords = @SNumToWords + ' and'
Select @STen = Substring(@SDecimal,1,1)
Select @SUnt = Substring(@SDecimal,2,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,9,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @ITen
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Select @SNumToWords = @SNumToWords + Space(1) + 'Paise'
End
End
--Print LTrim(RTrim(@SNumToWords))
return LTrim(RTrim(@SNumToWords))
END
выберите вот так
select dbo.usp_fnNumToWords(105422.00 ,'Y')
Y - с ps
N-без ps
С уважением,
приятель