Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String greater than or less than

Tags:

sql

I have the following data:

[sequences]
/a1
/a2
/a3
...
/a10

The query SELECT * FROM sequences WHERE nbr <= '/a10' should return the list above, instead it returns:

[results]
/a1
/a10

How do I make it return all the rows in the above list?

like image 903
mwok Avatar asked May 28 '11 21:05

mwok


People also ask

Can we use == for string comparison?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

How do you compare 2 strings?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

What does <= mean in JavaScript?

The less than or equal operator ( <= ) returns true if the left operand is less than or equal to the right operand, and false otherwise.


1 Answers

It works as it should. To compare the numeric value, you'll have to convert these to numbers somehow. A good start would be to use substr(yourfieldname, 3) to cut of the/a. Then you can useconvert` to typecast it to int, so your final query will look something like:

select * from sequences where convert(int, substr(nbr, 3)) <= 10

Mind that the exact functions and rules for converting strings to ints may very per dbms. This illustrates the general idea, though.

like image 198
GolezTrol Avatar answered Sep 21 '22 10:09

GolezTrol