Mysql query order by with longtext value as int - mysql

I tried below query it giving me error. what was the error
SELECT listingsdbelements_field_value
FROM default_en_listingsdbelements
ORDER BY CAST(listingsdbelements_field_value AS INT) ASC
Query: SELECT listingsdbelements_field_value FROM
default_en_listingsdbelements ORDER BY
CAST(listingsdbelements_field_value AS INT) AS...
Error Code: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'INT) ASC LIMIT 0, 1000' at line 3

Use signed or unsigned depending upon your needs.
SELECT listingsdbelements_field_value
FROM default_en_listingsdbelements
ORDER BY CAST(listingsdbelements_field_value AS UNSIGNED) ASC
From MySQL -
SIGNED Converts value to SIGNED type, which is a signed 64-bit integer
UNSIGNED Converts value to UNSIGNED type, which is an unsigned 64-bit integer

Related

Is there any way to return integer value from MySQL?

Is there any way to return integer value from mysql?
Select CAST(1, int) is thowing error as this "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT) limit 0,1000' at line 1"
Select CAST(0 as SIGNED) OR Select CAST(0 as UNSIGNED) both are returing BIGINT value.
I have tried different method to cast value to int.
I repost this answer
Create the function that will perform the conversion:
CREATE FUNCTION BigToInt (n BIGINT) RETURNS INTEGER RETURN n;
As you can see, the function is very short and simple: It takes a BIGINT and immediately returns it as an ordinary integer. However, one consequence of this is that some of the data will be truncated down to the largest possible value for Int.

How can I add a generated column in MySQL/MariaDB that will caculate a date from information in other columns?

I understand how to add a generated column using MariaDB (supports the same syntax as MySQL) and have done so with this table using a simple arithmetic operation. However when I try to use the DATE_SUB function I get the syntax error:
SQL Error [1064] [42000]: (conn=28594) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WEEKS)) STORED' at line 3
The code I am using is:
ALTER TABLE `ExampleTable`
ADD COLUMN `DOB` DATE
AS (DATE_SUB (`DateExpected`, INTERVAL `Age` WEEKS)) STORED;
I would like to calculate DOB from DateExpected and Age values.
From the CREATE TABLE:
`DateExpected` date DEFAULT NULL,
`Age` int(11) DEFAULT NULL,
Substituting with:
AS (DATE_SUB('1998-01-02', INTERVAL 31 DAY) STORED;
gives a similar error:
SQL Error [1064] [42000]: (conn=28594) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'STORED' at line 3
But substituting with:
AS (`DateExpected`) STORED;
works just fine at populating the DOB column with values from DateExpected, so I suppose this has to do with the DATE_SUB statement.
You can do:
ALTER TABLE `ExampleTable`
ADD COLUMN `DOB` DATE
AS (`DateExpected` - INTERVAL `Age` WEEK) STORED;

How to cast a string to numeric in mysql?

I have a table with string columns. Like varchar or text.
I want to select those values as a tinyint or int. But the following fails:
SELECT CAST(example AS INT) FROM mytable
Result:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'INT) FROM mytable LIMIT 0, 25' at line 1
Why?
Try using signed or unsigned:
SELECT CAST(example AS SIGNED) FROM mytable
I find it very strange that MySQL does not support INT in this context (or lengths on strings).
Also, in MySQL, implicit conversion is often used:
SELECT (example + 0)

storing Datetime in MySQL database

I will be getting datetime from JsonFile in "2019-05-03T06:45:06.000+0000" this format. I want to put this into DB.
I tried using datetime datatype in MySQL table. Its not working.
create table employee(
empId varchar(15),
requestorId varchar(15),
profile int ,
createdTime datetime,
reqId int
);
insert into employee values("x","y",1,2019-05-03T06:45:06.000+0000,2);
Error executing INSERT statement. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':45:06.000+0000,1)' at line 1 - Connection: Connection 1: 87ms
Use STR_TO_DATE, which can convert an input date string into a bona fide MySQL datetime value:
SELECT STR_TO_DATE('2019-05-03T06:45:06.000+0000', '%Y-%m-%dT%H:%i:%s.%f+0000');
The format mask used here is %Y-%m-%dT%H:%i:%s.%f+0000. Of note, the %f term matches fractional seconds. In the case of your timestamp, there are three places of fractional precision.

MySQL query with enum values

I've the following structure in MySQL 5.6.19.
CREATE TABLE `metadata` (
`md5` char(32) NOT NULL,
`match` enum('none','md5','similarity') DEFAULT NULL
)
And I got an error doing a query like this:
select * from metadata where match = 'md5';
The error is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '= 'md5'' at line 1
There're multiple entries in the table and rows that could match the query. But MySQL refuse to do it. Any idea about the reason?
Thanks!
MATCH is reserved keyword in MySQL: http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html. You should enclose your field name in backticks to make it work:
select * from metadata where `match` = 'md5';