--get text target starting integers into a table
declare @target TABLE([USERID] varchar(25),[target] int)
--get text stopping point integers into a table
declare @stop TABLE([USERID] varchar(25),[target] int, [stop] int)
--get just the options I want into a table
declare @options TABLE ([USERID] varchar(25), [userDetails] text)
insert into @options ([userid], [userDetails])
select u.userid, rtrim(ltrim(SUBSTRING([userDetails], s.[target], s.[stop] - s.[target])))
from users u join @stop s on u.userid = s.userid
declare @userDetails varchar(max)
Select top 1
@userDetails = [userDetails]
from @options
select charindex(char(32), @userDetails)
我从这个 charindex 得到的只是 0,我尝试过使用 ' ' 而不是 char(32)。 我尝试将文本更改为 varchar,但据我所知,这是毫无意义的,因为文本已被弃用并且已被视为 varchar?
当我将 userDetails 部分复制并粘贴到单引号字符串中并将其放入 charindex 函数时,一切都会按预期工作。
填充@userDetails的源文本如下所示:
key=value key=value key=value
最佳答案
在 SSMS 中切换到“结果到文本”,查看实际输出的内容。如果您处于“结果到网格”中,CRLF 将转换为空格。
正如 @PhilipKelley 在评论中提到的,
the move between columns gets converted to [tab] characters...
现在搜索 char(13) 或回车符可以让我找到每个 Key=Value 对之间的字符。它还解释了为什么有 2 个空格,这些是写入此数据的程序用来分隔每个条目的 CRLF
@Dems,提醒我
"White Space" covers tabs, carriage returns, and a whole host of other entities,
本网站上也讨论了类似的主题 another discussion
关于sql - 在 TEXT 列中搜索空格时,CHARINDEX 始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9384457/