SQL Server 在 IN 中使用通配符
因为我相信这应该是一个基本问题,所以我知道这个问题可能已经被问过,但我无法找到它。 我可能即将获得“同伴压力”徽章,但无论如何我都会问:
在 SQL Server 中是否有一种我不知道的方法可以在使用 IN 时使用通配符 %。
我意识到我可以使用 OR 之类的:
select *
from jobdetails
where job_no like '0711%' or job_no like '0712%'
在某些情况下,我可以使用子查询之类的:
select *
from jobdetails
where job_no in (select job_no from jobs where job_id = 39)
但我想做类似以下的事情:
select *
from jobdetails
where job_no in ('0711%', '0712%')
在这种情况下,它使用百分号作为字符而不是通配符,所以没有返回行。 目前,当我必须这样做时,我只使用一堆 OR,但我知道必须有更好的方法。 你用什么方法来做这个?
Since I believe this should be a basic question I know this question has probably been asked, but I am unable to find it. I'm probably about to earn my Peer Pressure badge, but I'll ask anyway:
Is there a way in SQL Server that I am not aware of for using the wildcard character % when using IN.
I realize that I can use OR's like:
select *
from jobdetails
where job_no like '0711%' or job_no like '0712%'
and in some cases I can use a subquery like:
select *
from jobdetails
where job_no in (select job_no from jobs where job_id = 39)
but I'm looking to do something like the following:
select *
from jobdetails
where job_no in ('0711%', '0712%')
In this case it uses the percent sign as a character instead of a wildcard character so no rows are returned. I currently just use a bunch of OR's when I have to do this, but I know there has to be a better way. What method do you use for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
尝试一下,
唯一的问题是
job '0713'
也会被返回因此可以使用
'07299999999999'
或仅添加和 job_no <> '0713'
丹·扎米尔
Try this
the only problem is that
job '0713'
is going to be returned as wellso can use
'07299999999999'
or just addand job_no <> '0713'
Dan zamir
我刚刚学习这些东西,但这有用吗?
I'm just learning this stuff but would this work?
这可能是最简单的解决方案,使用
像任何
一样在 Teradata 中工作正常。
This might me the most simple solution use
like any
In Teradata this works fine.
怎么样:
How about:
我想我已经以简单的形式找到了本次调查的发起人想要的解决方案。 它对我有用,实际上这就是我来到这里的原因。 我相信只需在列周围使用括号(如 '%text%')并结合
OR
就可以了。I think I have a solution to what the originator of this inquiry wanted in simple form. It works for me and actually it is the reason I came on here to begin with. I believe just using parentheses around the column like '%text%' in combination with
OR
s will do it.像这样的事情怎么样?
How about something like this?
您可以尝试这样的事情:
不完全是您所要求的,但它具有相同的效果,并且在其他方面也很灵活:)
You could try something like this:
Not exactly what you're asking, but it has the same effect, and is flexible in other ways too :)
我有一个类似的目标 - 并得出了这个解决方案:
我假设您的各种代码('0711%','0712%'等),包括%,存储在一个表中,我称之为* table_of_codes*,带有字段代码。
如果代码表中没有存储%,则只需连接“%”即可。 例如:
据我所知, concat() 函数可能会根据特定数据库的不同而有所不同。
我希望它有帮助。 我改编自:
http://us. Generation-nt。 com/answer/subquery-wildcards-help-199505721.html
I had a similar goal - and came to this solution:
I'm assuming that your various codes ('0711%', '0712%', etc), including the %, are stored in a table, which I'm calling *table_of_codes*, with field code.
If the % is not stored in the table of codes, just concatenate the '%'. For example:
The concat() function may vary depending on the particular database, as far as I know.
I hope that it helps. I adapted it from:
http://us.generation-nt.com/answer/subquery-wildcards-help-199505721.html
我首先添加了一个静态表,其中包含通配符结果的所有可能性
(该公司使用 4 个字符的 nvarchar 代码作为其所在地,并使用通配符来表示其所在地)
即他们可能有 456? 这会给他们 456[1] 到 456[Z] 即 0-9 & az
我必须编写一个脚本来拉出当前用户(声明他们)并拉出声明的用户的掩码。
创建一些临时表,只是基本临时表,以对当前用户的行号进行排名
循环遍历每个结果(您的或这个或者那个等等...)
插入到测试表中。
这是我使用的脚本:
I firstly added one off static table with ALL possibilities of my wildcard results
(this company has a 4 character nvarchar code as their localities and they wildcard their locals)
i.e. they may have 456? which would give them 456[1] to 456[Z] i.e 0-9 & a-z
I had to write a script to pull the current user (declare them) and pull the masks for the declared user.
Create some temporary tables just basic ones to rank the row numbers for this current user
loop through each result (YOUR Or this Or that etc...)
Insert into the test Table.
Here is the script I used:
正如杰里米·史密斯(Jeremy Smith)发布的那样,我将重述一下,因为我无法回答他的那个特定问题。
如果您只需要
0711%
和0712%
,您也可以在括号内放置一个范围。 对于NOT
关键字,您还可以使用[^1-2]%
As Jeremy Smith posted it, i'll recap, since I couldn't answer to that particular question of his.
If you just need
0711%
and0712%
you can also place a ranges within the brackets. For theNOT
keyword you could also use[^1-2]%
IN
运算符只不过是“=”比较的奇特OR
。 事实上,“只不过”是这样,在 SQL 2000 中,当列表包含大约 10k 条目时,由于将IN
扩展为OR
,因此出现了堆栈溢出错误(是的,有人写了 10k IN 条目...)。 所以你不能在其中使用任何通配符匹配。The
IN
operator is nothing but a fancyOR
of '=' comparisons. In fact it is so 'nothing but' that in SQL 2000 there was a stack overflow bug due to expansion of theIN
intoOR
s when the list contained about 10k entries (yes, there are people writing 10k IN entries...). So you can't use any wildcard matching in it.在 Access SQL 中,我会使用它。 我想 SQLserver 也有相同的语法。
选择 *
来自职位详细信息
其中 job_no 如“0711*”或 job_no 如“0712*”
In Access SQL, I would use this. I'd imagine that SQLserver has the same syntax.
select *
from jobdetails
where job_no like "0711*" or job_no like "0712*"
有点晚了,但你可以使用 STRING_SPLIT
将其变成可重用函数并不需要太多工作
A bit late to the party, but you could use STRING_SPLIT
It wouldn't take very much more work to turn that into a reusable function
我有同样的问题,并在 WHERE 子句中使用 SUBSTRING 函数找到了一个非常简单的解决方案。 这不会影响 job_no 在返回中的显示方式,而只是根据为子字符串函数输入的条件修剪 IN 搜索。
与 LEFT 或 RIGHT 相比,使用 SUBSTRING 的优点是您可以定义起始字符,因此如果您只想搜索字符串的中间,您可以这样做。
I had this same question and found a very simple solution using the SUBSTRING function in the WHERE clause. This will not affect how the job_no displays in the return, rather it just trims the IN search down based on the criteria entered for the substring function.
The advantage of using SUBSTRING versus LEFT or RIGHT is that you can define the starting character so if you just want to search the middle of a string, you can do so.
你的问题里就有答案了。 使用 IN 时不能直接传递通配符。 但是,您可以使用子查询。
试试这个:
我知道这看起来很疯狂,因为您可以坚持在 WHERE 子句中使用 OR 。 为什么是子查询? 然而,当您必须匹配来自不同来源的详细信息时,子查询方法将很有用。
拉吉
You have the answer right there in your question. You cannot directly pass wildcard when using IN. However, you can use a sub-query.
Try this:
I know that this looks crazy, as you can just stick to using OR in your WHERE clause. why the subquery? How ever, the subquery approach will be useful when you have to match details from a different source.
Raj