简体   繁体   English

T-SQL拆分字符串喜欢子句

[英]T-SQL Split String Like Clause

I have declare @a varchar(100) = 'abc bcd cde def' . 我已经declare @a varchar(100) = 'abc bcd cde def' What I need is to select from a table where a column is like 'abc' or 'bcd' or 'cde' or 'def'. 我需要从表中选择列,例如“ abc”或“ bcd”或“ cde”或“ def”。 I can use a split function and a while to get what I want, but somewhere I saw a smart solution using replace or something similar and I just can't remember it. 我可以使用split函数和while来获取想要的东西,但是在某个地方,我看到了一个使用replace或类似方法的智能解决方案,但我不记得了。

I know I can use an xml variable, and parse it that way. 我知道我可以使用xml变量,并以这种方式进行解析。 However, the value is part of a large procedure, and the best way for me is to use it in string form. 但是,该值是大型过程的一部分,对我而言,最好的方法是以字符串形式使用它。

I know I can solve this by building a dynamic sql query, but that is not an option in the domain I'm working in. 我知道我可以通过构建动态sql查询来解决此问题,但这不是我正在工作的域中的选项。

Damn, I just can remember the solution. 该死,我只记得解决方案。 Its a hack, a little dirty trick that do the job. 它是一个hack,可以完成这项工作的一个小技巧。

Anyways, I ll use the code bellow (Im over SQL Server 2008), is it a good idea? 无论如何,我将使用下面的代码(我通过SQL Server 2008),这是个好主意吗? I prefer it over the dirty split. 我喜欢它而不是肮脏的分裂。 Is it more performatic? 它表现得更好吗?

declare @w varchar(100) = 'some word'
declare @f xml

set @f = '<word>' + replace(@w, ' ', '</word><word>') + '</word>'

select
    template.item.value('.', 'varchar(100)') as word
from @f.nodes('/word') template(item)

In tsql you can use a pattern col like '[abcd]' 在tsql中,您可以使用模式col like '[abcd]'

http://msdn.microsoft.com/en-us/library/ms179859.aspx http://msdn.microsoft.com/en-us/library/ms179859.aspx

For matching multiple words (not letter) and without dynamic SQL, you'll have to get the values into a temp table. 为了匹配多个单词(不是字母)并且没有动态SQL,您必须将这些值放入临时表中。 For a split function try this page http://www.sommarskog.se/arrays-in-sql-2005.html#iterative and look at the List of Strings function iter_charlist_to_table. 对于拆分函数,请尝试此页面http://www.sommarskog.se/arrays-in-sql-2005.html#iterative,然后查看字符串列表函数iter_charlist_to_table。

Or maybe you are thinking of this little trick Parameterize an SQL IN clause from the SO CEO. 也许您正在考虑这个小技巧,可以通过SO CEO来参数化SQL IN子句

Use a function to split the individual items into a table, one record per item. 使用功能将单个项目拆分为一个表,每个项目一个记录。 Then you simply join to that table. 然后,您只需加入该表即可。

insert into #FilterTable (filters)
select Items from dbo.Split(@YourFilterString)

select * 
  from YourTable yt
  join #FilterTable f on f.filters = yt.YourColumn

Of course my example is using equality. 当然,我的例子是使用平等。 It gets more complicated if you truly intend to use "like" with wildcards. 如果您真正打算对通配符使用“ like”,则情况将变得更加复杂。

for 4 sections max 最多4节

WHERE
    PARSENAME(REPLACE(@a, ' ', '.'), 1) = 'xxx'
    OR
    PARSENAME(REPLACE(@a, ' ', '.'), 2) = 'xxx'
    OR
    PARSENAME(REPLACE(@a, ' ', '.'), 3) = 'xxx'
    OR
    PARSENAME(REPLACE(@a, ' ', '.'), 4) = 'xxx'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM