Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

sql server - SQL string comparison, greater than and less than operators

Today I viewed some query examples, and I found some string comparisons in the WHERE condition.

The comparison was made using the greater than (>) and less than (<) symbols, is this a possible way to compare strings in SQL? And how does it act? A string less than another one comes before in dictionary order? For example, ball is less than water? And this comparison is case sensitive? For example BALL < water, the uppercase character does affect these comparison?

I've googled for hours but I was not able to find nothing that can drive me out these doubt.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The comparison operators (including < and >) "work" with string values as well as numbers.

For MySQL

By default, string comparisons are not case sensitive and use the current character set. The default is latin1 (cp1252 West European), which also works well for English.

String comparisons will be case sensitive when the characterset collation of the strings being compared is case sensitive, i.e. the name of the character set ends in _cs rather than _ci. There's really no point in repeating all of the information that's available in MySQL Reference Manual here.

MySQL Comparison Operators Reference:
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html

More information about MySQL charactersets/collations:
http://dev.mysql.com/doc/refman/5.5/en/charset.html


To answer the specific questions you asked:

Q: is this a possible way to compare strings in SQL?

A: Yes, in both MySQL and SQL Server


Q: and how does it act?

A: A comparison operator returns a boolean, either TRUE, FALSE or NULL.


Q: a string less than another one comes before in dictionary order? For example, ball is less than water?

A: Yes, because 'b' comes before 'w' in the characteset collation, the expression

  'ball' < 'water'

will return TRUE. (This depends on the characterset and on the collation.


Q: and this comparison is case sensitive?

A: Whether a particular comparison is case sensitive or not depends on the database server; by default, both SQL Server and MySQL are case insensitive.

In MySQL it is possible to make string comparisons by specifying a characterset collation that is case sensitive (the characterset name will end in _cs rather than _ci)


Q: For example BALL < water, the upper case character does affect these comparison?

A: By default, in both SQL Server and MySQL, the expression

  'BALL' < 'water'

would return TRUE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...