Hab*_*bib 0 sql sql-server string sql-like
我想在多列中搜索一个字符串以检查它是否存在于任何列中。我在这里找到了一个解决方案Thorsten
的答案很短,但这是针对 mysql 服务器的解决方案,而不是针对 SQL Server。所以我想在 SQL Server 中应用类似的查询。
这是 Thorsten 建议的查询。
Select *
from tblClients
WHERE name || surname LIKE '%john%'
Run Code Online (Sandbox Code Playgroud)
我试过了
/* This returns nothing */
Select *
from Items
Where ISNULL(Code, '') + ISNULL(Code1, '') = '6922896068701';
Go
/* This generate error Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '|'.
I also used this one in mysql but it does not show the exact match.
*/
Select *
from Items
WHERE Code || Code1 = '6922896068701';
Go
/* This generate error Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near 'Or'. */
Select *
from Items
WHERE Code Or Code1 = '6922896068701';
Go
Run Code Online (Sandbox Code Playgroud)
在 SQL Server 中真的有可能吗?
注意:J__的答案在上面的问题链接中准确工作,但我希望为所有列输入一次比较字符串,就像 Thorsten 一样。
实际上,我认为在WHERE每个列的子句中进行单独的逻辑检查是这里的方法。如果由于某种原因不能这样做,请考虑使用WHERE IN (...)子句:
SELECT *
FROM Items
WHERE '6922896068701' IN (Code, Code1);
Run Code Online (Sandbox Code Playgroud)
相反LIKE,如果你想要逻辑,那么它就会变得棘手。如果您知道匹配的代码总是由数字/字母组成,那么您可以尝试:
SELECT *
FROM Items
WHERE ',' + Code + ',' + Code1 + ',' LIKE '%,6922896068701,%';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |