2017-04-26 14 views
0

数値を序数付きの単語に変換するスカラー関数をSQLサーバーに構築する方法。 例:入力25の出力は 、出力は'25番目 '数値を序数付きの単語に変換するSQL Serverスカラー関数

ありがとうございます。

+0

に賢明それは良い質問ですが、あなたは、データベースにそのようなことを処理しません。 – KumarHarsh

+0

これを行うには間違いなくデータベースが間違っていますが、クスクスにはこれに対処する方法がいくつかあります。あなたはどんな範囲を探していますか? 0〜999の間の任意の数ですか? – LordBaconPants

+1

[SQLサーバの単語機能を数値に書き込む方法](http://stackoverflow.com/questions/1673265/how-to-write-number-to-word-function-in-sql-server) – Mansoor

答えて

0

範囲0プログラミング9999

declare @t table(col1 int,col2 varchar(50),col3 int) 
insert into @t (col1,col2) VALUES (0,'Zero'),(1,'One'),(2,'Two') 
,(3,'Three'),(4,'Four'),(5,'Five'),(6,'Six'),(7,'Seven'),(8,'Eight') 
,(9,'Nine'),(10,'Ten'),(11,'eleven'),(12,'Twelve'),(13,'thirteen') 
,(14,'Fourteen'),(15,'Fifteen'),(16,'Sixteen'),(17,'Seventeen') 
,(18,'Eighteen'),(19,'Nineteen'),(20,'Twenty'),(30,'Thirty'),(40,'Forty') 
,(50,'Fifty'),(60,'Sixty'),(70,'Seventy'),(80,'Eighty'),(90,'Ninety') 
,(100,'Hundreds'),(1000,'Thousands'),(10000,'Thousands') 

    declare @input varchar(10)='1897' 

    declare @Words varchar(2000)='' 
    declare @i int=len(@input) 

    while((len(@input)>0)) 
    begin 

    if(@input>=0 and @input<=19 and @i=len(@input)) 
    begin 
    select @[email protected]+' '+ col2 from @t where col1 [email protected] 
    BREAK 
    END 
    else if(len(@input) between 3 and 4) 
    BEGIN 
    select @[email protected]+' '+ col2 from @t where col1 [email protected]/cast(('1'+replicate('0',len(@input)-1)) as int) 
    select @[email protected]+' '+ col2 from @t where col1 ='1'+replicate('0',len(@input)-1) 
    END 
    else 
    begin 
    select @[email protected]+' '+ col2 from @t where col1 =left(@input,1)+ replicate('0',@i-1) 

    END 
    set @input=stuff(@input,1,1,'') 
    if(cast(@input as int)=0) 
    BREAK 
    set @[email protected] 


    end 
    select @Words 
関連する問題