Sequelize - dateonly vs date format - mysql

If I store timestamp 1625555827900 in 2 separate fields whose format are dateonly and date,
will there be any difference?

You can not store the unix timestamps in both of the fields as their types are DATEONLY and DATE. Sequelize will through an error
DatabaseError [SequelizeDatabaseError]: date/time field value out of range: "1625555827900"
First you need to format the date like Tue Jul 06 2021 07:55:33 or 2021-07-06 07:55:33 in your code. In that case, it will save the complete date 2021-07-06 07:55:33.000000 for DATE field and skip the time part for DATEONLY field 2021-07-06.
Or you can use BIGINT instead if you are intended to store the unix timestamps.

Related

convert to 24 hour format in mysql

I have a query selecting time in the format where the time column is of the DateTime datatype.
SELECT ul.time FROM user_logins;
It is returning the result in this format-
11/27/2021 9:29:46 AM
11/23/2021 12:48:20 PM
Now I want it to return in the 24 hour format like
11/27/2021 9:29:46 11/23/2021 00:48:20
Is there any possible way to achieve that?
DATETIME values always stored in 24 hour format. As your column defined as DATETIME datatype, it's in 24 hour format
Output of DATETIME datatype:
2021-12-07 18:21:30.907
AM / PM is not mentioned by DATETIME, So We can format it.
Query: Select CONVERT(varchar,GETDATE(),9)
Output: Dec 7 2021 7:26:55:077PM

Converting date/time string to unix timestamp in MySQL

We have a database with all the dates and times stored in one column called timestamp. The format of the date/time in the column "timestamp" is as: 03 Aug 08:10am.
I would like to convert this (03 Aug 08:10am) to UNIX TIMESTAMP in MySQL and not PHP because we already have over 500 rows with this format: 03 Aug 08:10am.
I tried create a new INT column called new_timestamp and ran this query:
UPDATE table_name SET new_timestamp = UNIX_TIMESTAMP(timestamp);
However, it shows that 0 rows were affected.
This is not a duplicate, don't redirect me to how to convert in PHP. Read the question first :)
The UNIX_TIMESTAMP() function requires a valid date/time format to convert correctly, so you need to convert your existing date/time format to a valid/recognised format (including the year) first. You can do this using MySQL's STR_TO_DATE() function, telling it what format you are passing in, and concatenating in a hard-coded year value as it's always 2016 in your case.
STR_TO_DATE(CONCAT('2016-', <your date/time value>), '%Y-%d %b %h:%i%p')
You can then use the UNIX_TIMESTAMP() function to convert that valid date to your unix timestamp and update all those records in a single step:
UPDATE table_name
SET new_timestamp =
UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('2016-', timestamp), '%Y-%d %b %h:%i%p'));

Convert all columns of datetime to different date format in a table

I currently have a table, where three columns, named Start_Time, End_Time, and Scheduled_Time all have data format of Datetime where the format is YYYY-MM-DD 12:00:00 (24 hour clock). How would I be able to convert these columns where it would display as MM-DD_YYYY 12:00:00 (AM/PM) clock?
I believe that if you have a table with a column such as datefield DATETIME, in your select statement, if you CAST(datefield AS VARCHAR(20)), this will give you the proper time you are looking for(AM, and PM)
Testing it out myself I went from datefield being 2014-07-30 16:44:00 to Jul 30 2014 4:44PM by using CAST()

Convert date from serial number in csv file for mysql import

I am trying to import data from a csv file to a mysql table. One of the columns contains a serialized date (it came from another mysql table) which I need to convert to a format yyyy-mm-dd hh:mm:ss.000000. An example serial date is "1389792682". I tried converting in Excel but none of the custom formats recognize the number as a date - I believe because it's a date-time number. Any ideas?
Thanks
I think that you can use this formula in order to convert this timestamp to a date. Supposing your timestamp is in A1 cell:
=((A1/3600)/24) + DATE(1970,1,1)
This is because Unix epoch is the time 00:00:00 UTC on 1 January 1970.
And then chose to format your cell as a date.
The timestamp that you specifies in your question gives me:
year: 2014
month: 01
day: 15
h: 13:31
1389792682 corresponds to 2014-01-15 05:31:22 -0800 (2014-01-15 13:31:22 UTC) in Unix/Linux time.
It can be converted to a date field with
DATE_ADD('1970-01-01', INTERVAL 1389792682 SECONDS)

Convert Date to UTC timestamp in mysql

I have a table with the following columns:
|start_date |TZ |
|Dec 2, 2012 |Eastern |
|Dec 2, 2012 |GMT |
Note 1: our server is in UTC time.
Note 2:The column start_date is a date field, not a timestamp field. Dec 2nd 2012 implicitly means "2012-12-02 00:00:00"
Note 3: The above table is actually multiple normalized tables, but for simplicity, I de-normalized it.
Note 4: I can put anything into the TZ table to make this easy.
I would like to select from my_table where start_date <= now()
However, this doesn't work because of timezone. If the current date/time is
Dec 1st Eastern at 9PM (which is Dec 2nd 1AM UTC), the above query will return both results,
but I really only want the 2nd one. This is further complicated by daylight savings.
Ideally, I would like a query that does the following:
select * from my_table where convert_to_utc_timestamp(start_date,tz) <= now()
The above method would convert start_date to a timestamp and then convert it to the right timezone.
How would I do this in SQL?
There are two functions you'll probably find useful.
The first is:
STR_TO_DATE(start_date,'%M %d,%Y')
That will get your string, in the specified format, converted to a MySQL DATE datatype.
If you have the mysql.time_zone_name et al. tables populated, you can use the function:
CONVERT_TZ()
(need to check that CONVERT_TZ takes a DATE and will return a DATETIME or TIMESTAMP, or include a time component in the string being converted to get a DATETIME, e.g.
STR_TO_DATE( CONCAT(start_date,' 00:00:00'),'%M %d,%Y %T')
Wrap that expression in the CONVERT_TZ() function, e.g.
CONVERT_TZ( datetime_expr ,'US/Eastern','GMT')
To make use of the values stored in your TZ column, those are going to need to match, or you need to come up with a way to match to, the values stored in the mysql.time_zone_name table.