storing Datetime in MySQL database - mysql

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.

Related

How to update empty time in datetime MySQL

We are storing datetime in a column on our MySQL database, formatted in TEXT, but when our datetime is supposed to look like below:
'xxxx-xx-xx 00:00:00'
The time is deleted or not show on our datetime, and therefore our datetime, at that specific time, only contains the date:
'xxxx-xx-xx'
What we want is first of all to figure out why this is occurring, but for now we need to edit every row, and make sure the datetime is also showing the time. We have tried to change the impacted rows by using this query:
UPDATE table SET TIME(col_datetime) = '00:00:00' WHERE LENGTH(TIME(col_datetime)) = 0;
Above query should update the time on the datetime for col_datetime, where length of time is 0. Unfortunately, we receive an error, and we can't run the query. This is the error:
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 '(time_start) = '00:00:00' WHERE LENGTH(TIME(time_start)) = 0' at line 2
How can we change time on our datetime, where time is not shown?
Don't store dates as strings. Instead, you want to use the datetime datatype: it has a time part, that defaults to 00:00:00 when not specified.
Here is a small conversion script for that purpose:
alter table mytable add col_datetime_new datetime;
update mytable set col_datetime_new = col_datetime;
alter table mytable drop col_datetime;
alter table mytable change column col_datetime_new col_datetime datetime;
This leverages the fact that you are using format YYYY-MM-DD in your string dates, so conversion to datetime is seemless.

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.

Storing TIMESTAMP field in MySQL as datetime in SQL Server using Ruby

Im writing a job in my Rails application to migrate some data from a MySQL database to Microsoft SQL Server. In my orders table in MySQL the billing_date field is a TIMESTAMP, whereas the same field in SQL Server is of type datetime.
I have the following code:
def order_columns
%w{INVOICE_NUMBER BILLING_DATE}
end
invoice_number = #order.invoice_number
billing_date = #order.billing_date
values = [invoice_number, billing_date]
#query += "INSERT INTO ORDER (%s) VALUES (%s);" % [order_columns.join(","),values.join(",")]
The billing_date value in MySQL is 2016-09-02 08:43:10 . When the query gets executed i get the following error Incorrect syntax near '08'.
Is the query formulation correct ? If yes, what is the correct way of storing the billing_date of type TIMESTAMP to a datetime field in SQL Server ?

Time(0) giving syntax error in SQL

Hi I'm trying to create a new table and want to create a column with the following syntax:
CREATE TABLE tablename(
dinnerTime TIME(0) NOT NULL
);
I'm being told that I have an error in the SQL syntax, but shouldn't this just give me time in the format HH:MM:SS since I'm putting 0 as the precision?

MySQL select datetime

Just to understand the basics of MySQL: while the following works:
SELECT DATE('2010-01-01');
the following doesn't:
SELECT DATETIME('2010-01-01 12:34:56');
It raises the following:
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 '('2010-01-01 12:34:56')' at line 1
I wonder why it is so, as the documentation for DATE and DATETIME are respectively as follows:
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.
MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format.
I know that I do not need to "cast" a string into a DATETIME in order to insert it into a table. I am just wondering why this is not consistent.
mysql doesn't provide datetime function.
All it provide is TIMESTAMP()
SELECT TIMESTAMP('2010-01-01 12:34:56');
fiddle
Yup there is no DATETIME or similar name function in mysql.
Also DATE('2003-12-31 01:02:03') will only return the Date part in it.
Either use TIMESTAMP('2003-12-31 12:10:05') OR
STR_TO_DATE('5/15/2012 8:06:26 AM', '%c/%e/%Y %r') if you want to convert String to DateTime.