2017-04-26 40 views
0

如何在SQL服務器中構建標量函數,將數字轉換爲序數爲單詞的單詞。 例如: 對於輸入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