简体   繁体   中英

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

Here is the deal.

I have 2 databases. 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. 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. 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:

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

I could have sworn lower() was the right syntax. And I can't find anywhere on google saying any alternatives or why visual studio wouldn't like it. In fact I just tried a similar line in SQL Management Studio and it worked. Why is it not working in Visual Studio?

Use COLLATE to specify case sensitivity. 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. Also because you are using LIKE in your comparison what you actually want is something more 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. 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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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