简体   繁体   English

Lower()在Visual Studio 2010中SQL查询不起作用。 备择方案?

[英]Lower() In Visual Studio 2010 SQL Query Not Working. Alternatives?

Here is the deal. 这是交易。

I have 2 databases. 我有2个数据库。 One is older and has expanded data. 一个是较旧的并且已经扩展了数据。 The other is newer and has less relevant data. 另一个更新,相关数据较少。 They both share the same products just one has more data. 它们共享相同的产品,只有一个拥有更多的数据。

I've begun a project where I want to expand the newer database to include some of the missing data that exists in the older one. 我已经开始了一个项目,我想扩展新的数据库,以包含旧版数据库中存在的一些缺失数据。 Problem is the IDs don't match up between the databases. 问题是数据库之间的ID不匹配。 So I'm having to search by names. 所以我不得不按名字搜索。 Which may or may not be the same case. 哪个可能是也可能不是同一个案例。 The queries in visual studio are DEFINITELY case sensitive. visual studio中的查询完全区分大小写。 I tested this and I am sure. 我测试了这个,我很确定。

So my first thought was to do a like search with a lower function. 所以我的第一个想法就是用较低的功能进行搜索。 Like this: 像这样:

WHERE lower([Name1]) LIKE lower('%Name2%') 

but when I went to run it it gave me an error. 但是当我去运行它时它给了我一个错误。 And visual studio automatically tried to change the syntax of the statement to this: Visual Studio自动尝试将语句的语法更改为:

WHERE 'lower'([Name1]) LIKE 'lower'('%Name2%') 

I could have sworn lower() was the right syntax. 我可以发誓lower()是正确的语法。 And I can't find anywhere on google saying any alternatives or why visual studio wouldn't like it. 我无法在google上找到任何替代方案,或者为什么visual studio不喜欢它。 In fact I just tried a similar line in SQL Management Studio and it worked. 事实上,我刚刚在SQL Management Studio中尝试了类似的一行,但它确实有效。 Why is it not working in Visual Studio? 为什么它在Visual Studio中不起作用?

Use COLLATE to specify case sensitivity. 使用COLLATE指定区分大小写。 Simply force a case insensitive collation, if needed. 如果需要,只需强制不区分大小写的排序规则。 It may not be needed, depending on the existing collation of the data. 根据现有的数据整理,可能不需要它。 Eg.: 例如。:

SELECT ... FORM ...
WHERE Name1 COLLATE SQL_Latin1_General_CI LIKE '...';

First off VS does not execute the query, the collation on the column that you are querying will determine if SQL treats it as case sensitive or not. 首先,VS不会执行查询,您要查询的列上的排序规则将确定SQL是否将其视为区分大小写。 Also because you are using LIKE in your comparison what you actually want is something more like: 另外,因为你在比较中使用LIKE,你真正想要的更像是:

WHERE lower([Name1]) LIKE '%' + lower([Name2]) '%'

Use WHERE lower([Name1]) LIKE '%name2%' Since that value is constant (or input?) you don't need to convert it to lower here. 使用WHERE lower([Name1]) LIKE '%name2%'由于该值是常量(或输入?),因此您无需在此处将其转换为较低值。 You can do that beforehand. 你可以事先做到这一点。 Also, not sure about the [ ... ] . 另外,不确定[ ... ] They shouldn't be needed either. 也不需要它们。

But I think the best option would be to tell the database that you want to compare case insensitive, like so : 但我认为最好的选择是告诉数据库你要比较不区分大小写, 如下所示

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

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