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)
Related
I am new to COALESCE function in REDSHIFT. I ran below four queries in mysql and Redshift.
1st and 2nd query executed as expected in both mysql and redshift. But for 3rd and 4th query I am getting two different results in mysql and Redshift. How does this behave?
select COALESCE(null,null,1) -> 1
select COALESCE(null,null,'John') -> 1
select COALESCE(null,null,1,'John') -> (Redshift : error , mysql:1)
select COALESCE(null,null,'John',1) -> (Redshift: error, mysql:John)
Also this query should give error in mysql but it has succeeded
Any help is appreciated
Amazon Redshift Database Developer Guide claims:
An NVL expression is identical to a COALESCE expression. NVL and
COALESCE are synonyms.
Syntax
NVL | COALESCE ( expression, expression, ... )
An NVL or COALESCE expression returns the value of the first expression
in the list that is not null. If all expressions are null, the result
is null. When a non-null value is found, the remaining expressions in
the list are not evaluated.
This type of expression is useful when you want to return a backup
value for something when the preferred value is missing or null. For
example, a query might return one of three phone numbers (cell, home,
or work, in that order), whichever is found first in the table (not
null).
If you obtain the error this may mean that the returned value datatype do not match the datatype of recordset field or any another structure which must accept the returned value.
PS. Will you show error messages?
Though it is not written in the documentation, but coalesce works on the compatible data types. Integer and varchar cannot be compared.
The error becomes more evident when you provide column name instead of hard-code values. Try executing this:
select coalesce(integer_column, varchar_column) from a_table;
You would get an error saying something like this:
coalesce types integer and varchar cannot be matched.
I faced the issue related to min/max functions with a date.
For example - I get Varchar result datatype when querying min(Timestamp(dateColumn)), but I need Timestamp result datatype, I checked these functions with many different functions inside and it returns Varchar always, in every case except when I just call min(dateColumn). In the application I can't cast this operation every time, how could I cope with it?
I have tried to get a date from jdbc ResultSet but I can't track when I have to use result.getTimesatamp. It is impossible to track it.
I expect the min(timestamp('2019-01-01')) or max(timestamp('2019-01-01')) result with Timestamp/Date datatype, but actual result is Varchar result datatype.
Example - SELECT MIN(TIMESTAMP(orders.OrderDate)) FROM orders
The result will be with varchar datatype, but I need the timestamp.
Querying in workbench, dbeaver - results are the same
It is a bug in MySQL. You can do:
select max(x)
from (
select timestamp(now()) as x
) as x;
Or just use:
select min(dateColumn)
as use of the timestamp()-function is unnecessary in the query.
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
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
Wondering what's wrong with comparing BIGINT column values with '1'. Exactly, why this isn't producing any results:
SELECT * FROM table WHERE col = '1'
while this works just fine (i.e. returns non-empty result).
SELECT * FROM table WHERE col = 1
Thanks
If you are comparing an integer, you do not need quotes around it.
This appears to possibly be an unresolved bug.
See MySQL Bugs
You don't need to use quotes in comparing with Int or BigInt.
But I am getting result even I am comparing with quotes.
See this fiddle