Casting date to varchar in mysql throws syntax error - mysql

Why does MYSQL throw the following error on this simple query
select
cast(cast(ts as DATE) as varchar)
from
table_name
limit
1;
You have an error in your SQL syntax; it seems the error is around: 'varchar) from table_name limit 1' at line 2

The CAST() and CONVERT() functions don't support VARCHAR as a type.
They supports BINARY, CHAR, DATE, DATETIME, DECIMAL, DOUBLE, FLOAT, JSON, NCHAR, REAL, SIGNED [INTEGER], TIME, UNSIGNED [INTEGER]. See the manual.
You shouldn't have to cast a DATE to a string type anyway. In many client interfaces, it'll become a string in the result set.
Some interfaces may convert to language-specific types, like java.sql.Date.

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.

Error in SQL syntax when casting a column from int to varchar(32)?

So, I want to cast a column from int to varchar(32) as you can see below
SELECT CAST(`numbertwo` AS VARCHAR(32)) FROM `tablee`
But when entering the query, it reports, that I have an error in my SQL syntax.
Can you help me how I can fix that?
MySQL wants CHAR instead:
SELECT CAST(`numbertwo` AS CHAR) FROM `tablee`
You can specify a target length, in which case values that are too long are truncated (but no padding occurs for shorter values):
SELECT CAST(`numbertwo` AS CHAR(32)) FROM `tablee`
Reference: MySQL's CONVERT() function

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.

SQL CAST(int as varchar) throws error

Good evening,
I'm trying to convert an int to a varchar using the CAST() function but it gives me the following error:
syntax error, unexpected VARCHAR
and this is the code
CAST(p2v as varchar)
and i also tried
CAST(p2v as varchar(MAX))
I got the same problem when i try using CONVERT()
It must be char, not varchar
The type for the result can be one of the following values:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL[(M[,D])]
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
From http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html#function_cast