2015-08-24 44 views

回答

0

SQL-服務器你可以做到這一點與CASE聲明,你必須使用COALESCE爲空值轉換爲空字符串,是這樣的:

DECLARE @val INT = 123 

SELECT COALESCE(CASE WHEN @val LIKE '%0%' THEN 'Zero ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%1%' THEN 'One ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%2%' THEN 'Two ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%3%' THEN 'Three ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%4%' THEN 'Four ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%5%' THEN 'Five ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%6%' THEN 'Six ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%7%' THEN 'Seven ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%8%' THEN 'Eight ' END,'') + 
     COALESCE(CASE WHEN @val LIKE '%9%' THEN 'Nine ' END,'') 

OUTPUT:

One Two Three  

按照Gordon Linoff評論,你可以通過添加ELSE條款避免COALESCE

SELECT CASE WHEN @val LIKE '%0%' THEN 'Zero ' ELSE '' END + 
     CASE WHEN @val LIKE '%1%' THEN 'One ' ELSE '' END + 
     CASE WHEN @val LIKE '%2%' THEN 'Two ' ELSE '' END + 
     CASE WHEN @val LIKE '%3%' THEN 'Three ' ELSE '' END + 
     CASE WHEN @val LIKE '%4%' THEN 'Four ' ELSE '' END + 
     CASE WHEN @val LIKE '%5%' THEN 'Five ' ELSE '' END + 
     CASE WHEN @val LIKE '%6%' THEN 'Six ' ELSE '' END + 
     CASE WHEN @val LIKE '%7%' THEN 'Seven ' ELSE '' END + 
     CASE WHEN @val LIKE '%8%' THEN 'Eight ' ELSE '' END + 
     CASE WHEN @val LIKE '%9%' THEN 'Nine ' ELSE '' END 

UPDATE

按照意見,這將不會像「3421」號的工作,更新的代碼在情況下工作是這樣的:

DECLARE @val NVARCHAR(40) = '412' 
DECLARE @i INT = 1 
CREATE TABLE #Temp 
(  
    Val NVARCHAR(40), 
    IdRn INT 
) 
WHILE (@i < LEN(@val) + 1) 
BEGIN 
INSERT INTO #Temp VALUES 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '0' THEN 'Zero ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '1' THEN 'One ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '2' THEN 'Two ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '3' THEN 'Three ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '4' THEN 'Four ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '5' THEN 'Five ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '6' THEN 'Six ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '7' THEN 'Seven ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '8' THEN 'Eight ' ELSE '' END, @i), 
     (CASE WHEN SUBSTRING(@val,@i,1) LIKE '9' THEN 'Nine ' ELSE '' END, @i) 
SET @i = @i + 1 
END 

DECLARE @Nums VARCHAR(8000) 
SELECT @Nums = COALESCE(@Nums + ' ', '') + Val 
FROM #Temp 
WHERE Val <> '' 

SELECT @Nums 

輸出

Four One Two 
+0

需要將123轉換爲一個,兩個,三個......你想列出所有的數字嗎? –

+0

是的,我試過的情況下,但2或4位數的數字,我需要將它們分開 – Nikul

+0

之前誤解的問題,@Nikul檢查更新的答案 –

1

你可以做這樣的事情。我想不到更短的路。

這處理在輸入字符串中多次出現的數字。

DECLARE @Input varchar(max) 

SET @Input = '12307823' 

DECLARE @Result TABLE(Id INT IDENTITY(1,1), Value VARCHAR(1)) 
DECLARE @Mapping TABLE (Id int identity(0,1), Word varchar(6)) 
DECLARE @str VARCHAR(1) 
DECLARE @ind Int 

INSERT @Mapping (Word) VALUES 
         ('Zero '), ('One '),('Two '), ('Three '), 
         ('Four '), ('Five '), ('Six '), ('Seven '), 
         ('Eight '), ('Nine '), ('Ten ') 

IF(@Input is not null) 
BEGIN 
    SET @ind = 1 
    WHILE LEN(@Input) > 1 
    BEGIN 
      SET @str = SUBSTRING(@Input,@ind,1) 
      SET @Input = SUBSTRING(@Input,@ind+1,LEN(@Input)[email protected]) 
      INSERT INTO @Result values (@str) 
    END 
    SET @str = @Input 
    INSERT INTO @Result values (@str) 
END 

SELECT STUFF((SELECT Word 
    FROM @Result r INNER JOIN @Mapping m 
    ON r.Value = m.Id ORDER BY r.Id 
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, '') AS ConcatenatedString 
2

試試這個

declare @Input varchar(max)='123' 
declare @ouput varchar(max)='' 
declare @temp int=1 
declare @char nvarchar(max)='' 

WHILE @temp <=LEN(@Input) 
BEGIN 
set @char = SUBSTRING(@Input, @temp, 1) 
set @ouput += (case when @char=1 then 'One ' 
when @char=2 then 'Two '  
when @char=3 then 'Three ' 
else 'so on ' end) 
SET @temp = @temp + 1; 
END; 
print @ouput 
1

我會建議使用類似的東西來解釋:

--input 
DECLARE @input INT = 456 
-- create "dictionary" table 
DECLARE @dictionary TABLE(cKey INT, cValue VARCHAR(30)) 
-- create destination table 
DECLARE @dest TABLE(cInput INT, cID INT, cDigit INT, cValue VARCHAR(30)) 

--insert data into dictionary 
INSERT INTO @dictionary (cKey, cValue) 
VALUES(0, 'zero'), (1, 'one'), (2, 'two'), (3, 'three'), 
(4, 'four'), (5, 'five'), (6, 'six'), (7, 'seven'), (8, 'eight'), (9, 'nine') 

-- use CTE to split digits 
--- and insert data into destination table 
;WITH CTE AS 
(
    --initial value 
    SELECT 1 AS ID, @input AS InputValue, @input AS Digit, NULL AS Remainder 
    WHERE LEN(CONVERT(VARCHAR(30), @input))=1 
    UNION ALL 
    SELECT 1 AS ID, @input AS InputValue, CONVERT(INT, LEFT(CONVERT(VARCHAR(10), @input), 1)) AS Digit, RIGHT(CONVERT(VARCHAR(10), @input), LEN(CONVERT(VARCHAR(10), @input))-1) AS Remainder 
    WHERE LEN(CONVERT(VARCHAR(30), @input))>1 
    --recursive part 
    UNION ALL 
    SELECT ID+1 AS ID, InputValue, CONVERT(INT, LEFT(Remainder, 1)) AS Digit, RIGHT(Remainder, LEN(Remainder) -1) AS Remainder 
    FROM CTE 
    WHERE LEN(Remainder)>1 
    UNION ALL 
    SELECT ID+1 AS ID, InputValue, CONVERT(INT, Remainder) AS Digit, NULL AS Remainder 
    FROM CTE 
    WHERE LEN(Remainder)=1 
) 
INSERT INTO @dest (cID, cInput, cDigit, cValue) 
SELECT t1.ID, t1.InputValue, t1.Digit, t2.cValue 
FROM CTE t1 LEFT JOIN @dictionary t2 ON t1.Digit = t2.cKey 
ORDER BY t1.ID 

--display data 
SELECT DISTINCT t1.cInput, STUFF((SELECT ' ' + t2.cValue 
       FROM @dest t2 
       WHERE t2.cInput = t1.cInput 
       FOR XML PATH('')), 1, 1, '') AS DigitsAsWords 
FROM @dest t1 

結果:

cInput DigitsAsWords 
456  four five six 

毛皮療法的信息,請參閱:WITH Common Table Expressions

注:對於這樣的要求是不使用純SQL最好的解決辦法,但客戶端代碼。

1
declare @string as varchar(MAX) = 12845963254 
DECLARE @position int, @Kl As varchar(MAX) =''; 

SET @position = 1 
WHILE @position <= DATALENGTH(@string) 
    BEGIN 
    Set @Kl +=(Case CHAR(ASCII(SUBSTRING(@string, @position, 1))) 
       when 1 then ' One ' 
       when 2 then ' Two ' 
       when 3 then ' Three ' 
       when 4 then ' Four ' 
       when 5 then ' Five ' 
       when 6 then ' Six ' 
       when 7 then ' Seven ' 
       when 8 then ' Eight ' 
       when 9 then ' Nine ' 
       when 0 then ' Zero ' 
      end) 
    SET @position = @position + 1 
    END; 
    Select @Kl 
GO 
+0

也許關於它是如何工作的評論會很好。 – LJNielsenDk

相關問題