ปกติแล้ว sql server ถ้าเรา select อะไรก็ตามแล้ว where name = ‘oue’ ผลลัพธ์ที่ได้มาก็จะมีทั้ง oue, Oue, oUe, ouE เพราะว่า default เป็น non Case Sensitive แต่ถ้าผมต้องการให้ query เฉพาะ ‘oue’ ล่ะจะทำอย่างไร ก็ทำตาม code ด้านล่างเลยครับ
1
2
3
| select *
from user
where nickname COLLATE Latin1_General_CS_AS = 'oue' |
แค่นี้เอง สั้นๆง่ายๆ ครับ
When compare the string with not sure that the case of the string like (”aAa”, “aaa”, “AAA”). Normally I used the string.ToLower() to manage this problem but there’re many way to solved this. Look at my code below.
1
| string.Compare(s1, s2, true) |
This function return integer.
s1 > s2 then return 1
s1 < s2 then return -1
s1 = s2 then return 0
The 3rd pamerater is the "ignoreCase" then set it true to non-case sensitive
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
| string s1 = "aAa";
if (s1.Equals("aaa")) // false because the case sensitive
{
// do something
}
else if (s1 == "aaa") // false because the case sensitive
{
// do something
}
else if (string.Compare(s1, "aaa", true) == 0) // true enable non-case sensitive
{
// do something
} |