SQL statement convert varchar to integer - mysql

I was having some problem when trying to convert varchar field in MySQL to integer using SQL statement:
SELECT mrtpopTime, CONVERT(INT,mrtpopAmt)
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY CONVERT(INT, mrtpopAmt) DESC
I am trying to get the top 3 records when mrtpopAmt was arranged in a way where it is in reverse order. However, I am getting error message at the INT and the error message is:
Syntax error, Unecpected INT_SYM
I wonder why is it so? Thanks in advance.

This is because MySQL doesn't use CONVERT() for casting. It uses the CAST function. In your case you would use:
CAST(mrtpopAmt AS SIGNED) -- This can also be UNSIGNED if it will always be a positive integer

Related

SQL ignoring string in where condition

Why if I run a query with gibberish at the end of the where condition it incorrectly returns a result.
Example:
SELECT * FROM contractor_table WHERE contractorID = '97sd'
I am getting the row with the ID 97, when I should get no result.
What is happening here is that you are comparing the contractorID column, which is integer or some other numeric type, against a string literal 97sd. This doesn't make any sense, so MySQL, based on its casting rules, attempts to first cast 97sd to an integer. Consider the result of the following query:
SELECT CAST('97sd' AS unsigned);
In fact, this outputs just 97, the integer. So, in practice this means that the "gibberish" at the end of your string literal, which begins with an integer, will be ignored by MySQL.
But best practice here is to always compare columns against the correct literal types. So use the following version always, for best results:
SELECT * FROM contractor_table WHERE contractorID = 97;
This happends when you have column type int or other numeric if you convert it into varchar than it will retun no output

SQL converts 'a' to 0 on a integer column. Can i prevent this?

SQL converts a string ('a') to integer (0) on a integer column as learned before in:
Why does SELECT ... WHERE id = a returns a result if value is 0
Is there a way to prevent this? For example: SELECT ... WHERE id = NO_CONVERT_STR('a') or something like this? I don't want to change the data type of the id column
I tried the CAST('a' AS CHAR) method without luck
MySQL has an option for turning off such conversions on inserts. I don't believe there is an option to turn off silent conversion.
You can, however, force an error by doing an explicit conversion:
where id = convert('a' as unsigned)
This should generate an error.

MySQL automatic string to integer casting in where clause?

I am not new to MySQL but a strange situation happened in my code today coincidently which got me surprised. Can someone explain why this gives me identical results?
SELECT * FROM `products` WHERE id = 12
and
SELECT * FROM `products` WHERE id = '12ABC'
In both cases I get the same result with the same record being selected. I would expect that second one would return me nothing?! My ID field is int(11) unsigned with auto_increment flag turned on.
From MySQL docs:
When an operator is used with operands of different types, type conversion occurs to make the operands compatible
Documentation
So basically, '12ABC' is cast to 12.
MySQL has to make a conversion to make a compare betwen 2 different types. It tries to make the string to an int and get the digits from the string starting from the beginning.
It you had for instance
'ABC12'
the result of the string conversion to int would be 0

MySQL Cast operation throws generic error

This statement is part of a larger statement but this past is not work. It throws the usual 1064 error. The goal is to parse a string to get at the number that is in the middle of it, then to cast this to an int so that said number can be compared against other id values. This is the failing query
SELECT CAST(REPLACE(REPLACE('remove_this-151-remove_this_too', 'remove_this-', ''), '-remove_this_too', '') as INTEGER);
I'm not sure why this type of syntax is not allowed. Thank you to anyone who can help.
You need to do that with:
SELECT CAST(REPLACE(REPLACE('remove_this-151-remove_this_too', 'remove_this-', ''), '-remove_this_too', '') as UNSIGNED)
-since there's no INTEGER modifier for CAST() in MySQL, but there are SIGNED and UNSIGNED

how to compare two varchar values with leading zeros in mysql

I have to compare 2 columns in mysql which has varchar data type. But there are some data which has leading '0' which is creating problem while comparing.
For ex: I have to compare between '02653' and '2653' which are actually equal, but here my query fails and gives different result.
Please suggest any idea what should be the right query for this.
I have tried to run like this
SELECT DISTINCT table1.store_id
FROM table2
WHERE
CONVERT('first_column' AS INTEGER) = CONVERT('second_column' AS INTEGER)
Showing this error
** for the right syntax to use near AS INTEGER **
convert the varchar to integer and then compare
...
WHERE CONVERT(column1 AS INTEGER) = CONVERT(column2 AS INTEGER)