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
Related
I took over an old SQL query that includes the following expression:
SELECT
g.Citystate,
g.Cityorder,
if(CityPostal is null, CityName, concat(CityName, ', ', CityPostal)) as CityName,
g.CityName as CityAdress,
g.CityKey,
#Published = ifnull(if (g.CityKey='S',
(SELECT sum(if(FROM_UNIXTIME(g1.Timestamp/1000) >= STR_TO_DATE('$VARIANT$', '%e.%m.%Y'), g1.Published, 0))
from PUBLISHED_DATA g1
If I execute the whole SQL Statement in DBeaver I get the following error:
Incorrect datetime value: '$VARIANT$' for function str_to_date
I think the error occures because the syntax did not work with the newest MySQl version.
The query works if I pass a static string like "01.01.1971" as first argument to the STR_TO_DATE function. I'm searching for how to set up dynamic variable names, but I didn't find any solution. What does $VARIANT$ exactly mean? Does it define a parameter?
i need to cast from varchar(100) to bigint that way:
select max(cast(referencia_factura as bigint))from ventas;
but i get a syntax error. ¿how can i solve it?
I need to get the maximum value of the "referencia_factura" field but I need to cast it to bigint.
A simple method is to use implicit conversion:
select max(referencia_factura + 0)
from ventas
Unfortunately, MySQL does not support direct conversion to bigint.
As it is explained in Cast Functions and Operators, casting to BIGINT is not supported by MySql.
I guess that referencia_factura's values are too long so they can't be converted to UNSIGNED and also an implicit conversion like referencia_factura + 0 would fail.
In this case what you can do is left pad the column values with 0s up to the max length of the column which is 100 and compare them as strings to find the maximum and then trim all the leading 0s:
SELECT TRIM(LEADING '0' FROM MAX(LPAD(referencia_factura, 100, '0')))
FROM ventas;
See the demo.
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
I'm working with nested sets for my CMS but since MySQL 5.5 I can't move a node.
The following error gets thrown:
Error while reordering docs:Error in MySQL-DB: Invalid SQL:
SELECT baum2.id AS id,
COUNT(*) AS level
FROM elisabeth_tree AS baum1,
elisabeth_tree AS baum2
WHERE baum2.lft BETWEEN baum1.lft AND baum1.rgt
GROUP BY baum2.lft
ORDER BY ABS(baum2.id - 6);
error: BIGINT UNSIGNED value is out of range in '(lektoren.baum2.id - 6)'
error number: 1690
Has anyone solved this Problem? I already tried to cast some parts but it wasn't successful.
BIGINT UNSIGNED is unsigned and cannot be negative.
Your expression ABS(lektoren.baum2.id - 6) will use a negative intermediate value if id is less than 6.
Presumably earlier versions implicitly converted to SIGNED. You need to do a cast.
Try
ORDER BY ABS(CAST(lectoren.baum2.id AS SIGNED) - 6)
SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';
Call this before the query is executed.
ORDER BY ABS(CAST(lectoren.baum2.id AS BIGINT SIGNED) - 6)
That change would be mysql only.
instead, do
ORDER BY ABS(- 6 + baum2.id);