SQL - 如何return特定单词后的一组数字
SQL - How to return a set of numbers after a specific word
在 SQL 中,我试图 return 特定单词后的第一组数值。我只想要特定单词后字符串中的数字。
例如:'你好:'
例如:
hello : 123 should return 123
hello : x123 should return 123
hello : 123 456 should return 123
cake : 10 should not return anything
到目前为止我已经解决了我需要做的事情 -
Declare @sInitialString varchar(max)
@sInitialString = " hello hello : 123 456"
--FIND ' hello : ' within @sInitialString
-- possibly save this a new substring or string?
-- find the first numerical value after this has been found
看起来很简单,但从以前的帖子来看,它似乎更复杂。
我已经设法将所有数值设为 return
DECLARE @sInitialString VARCHAR(MAX)
SET @sInitialString = (SELECT UPPER(' hello hello : 123 '))
select substring(@sInitialString,patindex('%[0-9]%',@sInitialString),100)
我的方法或解决方案似乎遗漏了一些东西。有没有人设法做到这一点?
假设您的代码可以找到相关字符串,您可以使用 outer apply
:
获取第一个值
select x.nums
from (select substring(@sInitialString, patindex('%[0-9]%', @sInitialString), 100) s
) s outer apply
(select (case when s.s like '% %'
then left(s.s, charindex(' ', s.s)
else s.s
end) as nums
) x
不过我不认为你的逻辑真的有效,因为它不是在寻找 hello
。所以,您可能正在寻找更像的东西:
select x.nums
from (select (case when @sInitialString like 'hello%[0-9]%'
then substring(@sInitialString, patindex('%[0-9]%', @sInitialString), 100)
end) s
) s outer apply
(select (case when s.s like '% %'
then left(s.s, charindex(' ', s.s))
else s.s
end) as nums
) x;
尝试以下解决方案。为了使最后的 PATINDEX 搜索有效,我必须在末尾添加一个 space 符号。我使用 2 步使代码可读,但您可以将其转换为单个语句以在 SELECT 中使用或使用 CTE 实现多个步骤。
DECLARE @sInitialString VARCHAR(MAX) = ' hello hello : retert 123'
DECLARE @sToken VARCHAR(MAX) = 'hello :'
-- Add a character at the to make search of the numeric string end work
SELECT @sInitialString += @sInitialString + ' '
-- Find String token and save the rest of the string to the variable
SELECT @sInitialString = SUBSTRING(@sInitialString, PATINDEX('%' + @sToken + '%', @sInitialString) + LEN(@sToken), 10000)
-- The extract string from first numeric character unitl last numeric
SELECT @sInitialString = SUBSTRING(@sInitialString, PATINDEX('%[0-9]%', @sInitialString), PATINDEX('%[0-9][a-z !@#$%^&*(()_]%', @sInitialString) - PATINDEX('%[0-9]%', @sInitialString) + 1)
SELECT @sInitialString
在 SQL 中,我试图 return 特定单词后的第一组数值。我只想要特定单词后字符串中的数字。 例如:'你好:'
例如:
hello : 123 should return 123
hello : x123 should return 123
hello : 123 456 should return 123
cake : 10 should not return anything
到目前为止我已经解决了我需要做的事情 -
Declare @sInitialString varchar(max)
@sInitialString = " hello hello : 123 456"
--FIND ' hello : ' within @sInitialString
-- possibly save this a new substring or string?
-- find the first numerical value after this has been found
看起来很简单,但从以前的帖子来看,它似乎更复杂。
我已经设法将所有数值设为 return
DECLARE @sInitialString VARCHAR(MAX)
SET @sInitialString = (SELECT UPPER(' hello hello : 123 '))
select substring(@sInitialString,patindex('%[0-9]%',@sInitialString),100)
我的方法或解决方案似乎遗漏了一些东西。有没有人设法做到这一点?
假设您的代码可以找到相关字符串,您可以使用 outer apply
:
select x.nums
from (select substring(@sInitialString, patindex('%[0-9]%', @sInitialString), 100) s
) s outer apply
(select (case when s.s like '% %'
then left(s.s, charindex(' ', s.s)
else s.s
end) as nums
) x
不过我不认为你的逻辑真的有效,因为它不是在寻找 hello
。所以,您可能正在寻找更像的东西:
select x.nums
from (select (case when @sInitialString like 'hello%[0-9]%'
then substring(@sInitialString, patindex('%[0-9]%', @sInitialString), 100)
end) s
) s outer apply
(select (case when s.s like '% %'
then left(s.s, charindex(' ', s.s))
else s.s
end) as nums
) x;
尝试以下解决方案。为了使最后的 PATINDEX 搜索有效,我必须在末尾添加一个 space 符号。我使用 2 步使代码可读,但您可以将其转换为单个语句以在 SELECT 中使用或使用 CTE 实现多个步骤。
DECLARE @sInitialString VARCHAR(MAX) = ' hello hello : retert 123'
DECLARE @sToken VARCHAR(MAX) = 'hello :'
-- Add a character at the to make search of the numeric string end work
SELECT @sInitialString += @sInitialString + ' '
-- Find String token and save the rest of the string to the variable
SELECT @sInitialString = SUBSTRING(@sInitialString, PATINDEX('%' + @sToken + '%', @sInitialString) + LEN(@sToken), 10000)
-- The extract string from first numeric character unitl last numeric
SELECT @sInitialString = SUBSTRING(@sInitialString, PATINDEX('%[0-9]%', @sInitialString), PATINDEX('%[0-9][a-z !@#$%^&*(()_]%', @sInitialString) - PATINDEX('%[0-9]%', @sInitialString) + 1)
SELECT @sInitialString