SQL CAST(int as varchar) throws error - mysql

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

Related

Error converting date type in MySQL (ERROR 1411)

I have this column called as "orderdate" that has varchar data type. I want to convert it to date using this
update january_2019 set orderdate = str_to_date(orderdate,'%m/%d/%y');
but eventually it generates error
Error Code: 1411. Incorrect datetime value: 'Order' for function str_to_date
Any help?

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

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.

Casting date to varchar in mysql throws syntax error

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.

MySQL Unable to insert WHERE STR_TO_DATE IS NULL

I am trying to insert VARCHARs strings which are not a valid date format into another table where they are also stored as a VARCHAR. I'm trying to use STR_TO_DATE to accomplish this, which is supposed to return NULL if a string's format is invalid:
INSERT INTO DateErrors
SELECT `row_id`,`date_string`
FROM `source_table`
WHERE STR_TO_DATE(`date_string`,'%d-%m-%Y') IS NULL
Which throws an error for strings with an invalid date format:
Error Code: 1411. Incorrect datetime value: 'some random string that isnt a date' for function str_to_date. Instead of throwing this error, I want these string values inserted into DateErrors.
However, running just the SELECT query returns the rows where date_string is invalid without throwing an error.
You are specifying wrong format for your date string in STR_TO_DATE
STR_TO_DATE(`date_string`,'%d-%m-%Y')
instead, it should be
STR_TO_DATE(`date_string`,'%Y-%m-%d')