How to convert date into timestamp in SQL query? - mysql

I'm trying to migrate data from old_table to new_table, but in old table the date datatype is datetime and in new table the date datatype is int and is accepting timestamp value.
So how to convert the old date in sql query so that it get inserted in new table as timestamp?
INSERT INTO `new_table` (`id`, `user_id`, `doctor_id`, `message_id`, `type`, `is_message`, `is_note`, `doctor_initials`, `call_status`, `message`, `date_created`, `date_updated`, `day`)
SELECT id, usid, drid, message_id, type, is_message, is_note, doctor, kall, message, datein, 123, ziua
FROM `old_table`;
I want a function which convert old date to value to timestamp e.g in the above query CONVERT_INTO_TIMESTAMP(datein)
Any help will be highly appreciated.
New Table date_created field accepts values in unix timestamp such as : 1540642765
Thanks

The function you are looking for is UNIX_TIMESTAMP(), e.g.
select UNIX_TIMESTAMP('1970-01-01 00:00:00');
gives 0 in the UTC timezone.

Try UNIX_TIMESTAMP() function:
INSERT INTO `new_table`
(`id`,
`user_id`,
`doctor_id`,
`message_id`,
`type`,
`is_message`,
`is_note`,
`doctor_initials`,
`call_status`,
`message`,
`date_created`,
`date_updated`,
`day`)
SELECT id,
usid,
drid,
message_id,
type,
is_message,
is_note,
doctor,
kall,
message,
Unix_timestamp(datein),
'123',
ziua
FROM `old_table`;
From Docs:
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP()
is called with a date argument, it returns the value of the argument
as seconds since '1970-01-01 00:00:00' UTC. The date argument may be a
DATE, DATETIME, or TIMESTAMP string, or a number in YYMMDD,
YYMMDDHHMMSS, YYYYMMDD, or YYYYMMDDHHMMSS format. The server
interprets date as a value in the current time zone and converts it to
an internal value in UTC.

SELECT date_part('epoch', CURRENT_TIMESTAMP)::int;

since this question is also tagged for SQL
people who came here for timestamp solution in SQL/MSSQL Query. can try below
SELECT CAST(DueDate AS TIMESTAMP) "NewDueDate"
where DueDate is column with having date like - YYYY-MM-DD HH:ii:ss .
if your column is in type varchar then still above syntax will work fine.

Related

How to insert the current date on my table?

How to insert this date format on my table: 30/12/2018?
If it's impossible, then how can i turn this date format: 2018-12-30 12:10:00 to 30/12/2018 on a echo?
Store the date/time in the native format (i.e. as a datetime or date). Then, use date_format() to convert it to the format you want on output:
select date_format(datecol, '%d/%m/%Y')
You should proceed as follows :
ensure that the field where you store the date is of type datetime or date
use function STR_TO_DATE to convert strings to dates before writing to database
use function DATE_FORMAT to format the datetime values to the relevant format when reading form database.
Here is a small example of CREATE/INSERT/SELECT :
CREATE TABLE mytable (
mydate datetime
);
INSERT INTO mytable
VALUES (STR_TO_DATE('30/12/2018', '%d/%m/%Y'));
SELECT DATE_FORMAT(mydate, '%d/%m/%Y')
FROM mytable;

MySQL incorrect timestamp value

I'm trying to insert datetime value '1970-01-01 00:00:01' in timestamp column but MySQL returned an error "Incorrect datetime value: '1970-01-01 00:00:01' for column 'timestamp'"
CREATE TABLE TST_TABLE
(
tst_column timestamp NULL
)
INSERT INTO TST_TABLE(tst_column) VALUES('1970-01-01 00:00:01');
I'm confused because MySQL documentation claims that lowest valid value for timestamp is '1970-01-01 00:00:01'. What's wrong and what is real lowest timestamp value?
Thanks.
This is a timezone issue. Set the timezone to UTC before the insert, for example :
SET time_zone='+00:00';
INSERT INTO TST_TABLE(tst_column) VALUES('1970-01-01 00:00:01');
An other option is to convert you timestamp to the UTC timezone using CONVERT_TZ. For exemple, if your timezone is Europe/Paris :
INSERT INTO TST_TABLE(tst_column) VALUES(CONVERT_TZ('1970-01-01 00:00:01', 'Europe/Paris', 'UTC'));

MySQL: Select between dates dd.mm.yyyy hh.mm.ss

what mysql syntax I can use to select data from a mysql table based on "between dates"?
I have a column that contains only dates in format like: 08.05.2016 18:09:31 (dd.mm.yyyy hh.mm.ss)
I am assuming you made an early error in your database design and put a date and time into a varchar or some kind of string column type.
So in that case you need to convert the string into a proper MYSQL DateTime and then you can use the BETWEEN syntax
SELECT * FROM TABLE
WHERE STR_TO_DATE(sillyDateColumn, '%d.%m.%Y %h:%i:s')
BETWEEN '2016-01-01 00:00:01' AND '2016-01-02 00:00:01'
For future reference always store Dates or Time or DateTime values in appropriate MYSQL data types i.e. DATE or TIME or DATETIME
If your users want to see these values presented differently on there systems, format the dates to locale specific formats for and in the presentation layer only.
If I am wrong and these dates are in a DATETIME type column then all you need to do is
SELECT * FROM TABLE
WHERE DateColumn BETWEEN '2016-01-01 00:00:01' AND '2016-01-02 00:00:01'

Datetime field overflow error in mysql

I am trying to insert into a mysql table after conversion from mssql. The datetime format was in hexadecimal, Then i wrote a function for converting it to datetime. When i try to insert the values, I am getting datetime field overflow error.
My insert statement looks like this:
INSERT into BankMaster (BankCode, BankName, CREATEDID, CREATEDDATETIME, UPDATEDID, UPDATEDDATETIME) VALUES ('N','xxxx','admin',cast(ConvertSQLServerDate(0x98A90076) as datetime), NULL, NULL);
Function is as follows:
create function ConvertSQLServerDate(dttm binary(16))
returns datetime
return CAST(
'1900-01-01 00:00:00' +
INTERVAL CAST(CONV(substr(HEX(dttm),1,8), 16, 10) AS SIGNED) DAY +
INTERVAL CAST(CONV(substr(HEX(dttm),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND AS DATETIME);
Any help would be highly appreciated.
I had generated script with smalldatetime as data type in MSSQL. So I was getting an 8bit hexadecimal number. Later on, I converted into datetime datatype which resulted in 16bit hexadecimal value and I got my desired result.

Convert string datetime in non-standard format into DATETIME on INSERT into MySQL

If I have a string that represents the date and time such as '30/09/2011 10:14' which is a non-standard DATETIME format. Can I reformat this to a standard DATETIME format on insert into a DATETIME field in a MySQL database table?
I have tried this:
INSERT INTO test(lastdate) VALUES(DATE_FORMAT('30/09/2011 10:14', '%d/%m/%Y %H:%i'))
The result is NULL in the lastdate DATETIME field.
You can use str_to_date to convert for your insert :
INSERT INTO test(lastdate) values (str_to_date('30/09/2011 10:14',"%d/%m/%Y %h:%i"))
DATE_FORMAT is for translating a date / time field to another format on select
Change date_format to str_to_date and you'll be good to go.