如何将SQL Server中的单词联合起来?

时间:2017-02-09 06:33:59

标签: sql-server tsql

我有一个标量值

八万七千九百九十九

我怎样才能成功八十七万九百八十九

到目前为止,我已经找到了发生的位置,但之后我就迷失了

with cte as
(select 'Eighty Thousand and Seven Thousand and Nine Hundred and Eighty Nine' as name
), 
pos as
(select patindex('%Thousand%',name) pos, name from cte
union all
select pos+patindex('%Thousand%',substring(name, pos+1, len(name))) pos, name from pos
where patindex('%Thousand%',substring(name, pos+1, len(name)))>0
)
select pos from pos

1 个答案:

答案 0 :(得分:0)



Declare @String1 varchar(100) Declare @String2 varchar(100) declare @string nvarchar(264)
set @string = N'Eighty Thousand and Seven Thousand and Nine Hundred and Eighty Nine';
-- Ensure it ends with a space
SET @string = @string + ' ';
WITH cte (Id, Value, Rest)
AS (SELECT 1 as Id,SUBSTRING(@string, 1, CHARINDEX(' ', @string, 1) - 1) AS Value ,SUBSTRING(@string, CHARINDEX(' ', @string, 1) + 1, LEN(@string)) AS Value
    UNION ALL SELECT Id + 1 AS Id,SUBSTRING(Rest, 1, CHARINDEX(' ', Rest, 1) - 1) AS Value,SUBSTRING(Rest, CHARINDEX(' ', Rest, 1) + 1, LEN(Rest)) AS Value  FROM cte WHERE  CHARINDEX(' ', Rest, 1) <> 0)

SELECT @String1= Value
FROM cte
WHERE id = 2; -- = 2rd word to get
WITH cte1 (Id, Value, Rest)
AS (SELECT 1 as Id,SUBSTRING(@string, 1, CHARINDEX(' ', @string, 1) - 1) AS Value ,SUBSTRING(@string, CHARINDEX(' ', @string, 1) + 1, LEN(@string)) AS Value
    UNION ALL SELECT Id + 1 AS Id,SUBSTRING(Rest, 1, CHARINDEX(' ', Rest, 1) - 1) AS Value,SUBSTRING(Rest, CHARINDEX(' ', Rest, 1) + 1, LEN(Rest)) AS Value FROM cte1 WHERE  CHARINDEX(' ', Rest, 1) <> 0)
SELECT @String2 =Value
FROM cte1
WHERE id = 5; -- = 5rd word to get
IF @String1=@String2
Begin set @string = Replace(Replace(@string,'Thousand and',''),'  ',' ') Print @string
END 
&#13;
&#13;
&#13;