The timestamp of the nodes in my node table have year as 201 instead of 2014 which are stored in unixtime. Is there a way in which I can update only the year of all the nodes concerned?
MySQL timestamp field allows a range of values between -2147483648 and 2147483648. (A detailed explanation can be found here.) The Unix time equivalent of "201-01-01 00:00:00" is
-55824249600. So, MySQL timestamp field cannot save a date from year 201.
If my above assumption is wrong and a date from year 201 exists in a timestamp field on your database, I think the best solution is;
Calculate the interval between 201-01-01 00:00:00 and 2014-01-01 00:00:00 in seconds:
1388534400 - (-55824249600) = 57212784000
Add this value to all "created" fields in your "node" table with the following SQL query:
UPDATE node SET created = created + 57244320000;
Related
I have a MySql DB that consists of a table that holds all my data. I need to retrieve only those rows that have a startup_date that is less than or equal to today at 5 am UTC time. I tried looking up but it's kind of confusing. Can someone provide me a good, clean way of doing this?
My table is as follows:
tbl_MyData
***********
id name city startup_date
1 test1 New York 2020-01-10 18:19:30
2 test2 Houston 2019-01-30 05:00:00
3 test3 Chicago 2020-02-09 05:00:00
From the above data, my query should return ONLY row id number 2, since that is the only row that satisfies my criteria of startup_date being less than or equal to today at 5 am. Also, I'm not sure if I need to have any kind of UTC functions, since I'm not sure how mysql stores it's date time data.
Use a simple WHERE clause
SELECT t.* FROM mytable WHERE t.startup_date <= DATE_ADD(CURDATE(), INTERVAL 5 HOUR)
The CURDATE() function returns the current date without time (hence today at midnight), and the DATE_ADD() function adds 5 hours to it.
What SELECT statement have you tried also what are the data types for the table? If startup-date is a date type in MySQL then it should just be
SELECT * FROM tbl_MyData WHERE startup_date<=(whatever the value needs to be)
I have a datetime 0000-00-00 00:00:00 column in my table,
Need to keep the minute and seconds when updating ____-__-__ __:MM:SS
but also change the date and hour to current time.
How can I achieve this on MySQL side?
Edit (Sample):
Current Date field value: 2016-06-27 15:13:07
We Update this table at 2016-07-28 12:31:18
Desired Date field value: 2016-07-28 12:13:07
As you can see the updated date still has correct 2016-07-28 12: but the :13:07 (minutes and seconds) are Selected from the date before update and replaced by current time's minutes and seconds
You can make use of MySQL CONCAT() function. Your update query would looks something like this,
UPDATE table_name
SET
date_column = CONCAT('2016-06-29 15:',MINUTE(date_column),':',SECOND(date_column))
WHERE
column_id = 1
CONCAT() returns the string that results from concatenating the given arguments.
So in your case we would update date and hour by adding first argument as 2016-06-29 15:, and then use the same minute and second from the same column. And concatenate all the arguments to make the new value as you need.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
This question already has answers here:
Can MySQL convert a stored UTC time to local timezone?
(6 answers)
Closed 6 years ago.
How do I SELECT all records from yesterday, from Midnight to Midnight, for a specific location's timezone (timestamps all in UTC and locations have named timezones - e.g. America/Vancouver)?
Please note that I'm not asking how to convert a date and time to another timezone. What I'm asking is how [best] to localize a date range comparison.
Assuming the timezone for your MySQL database session is UTC (i.e. time_zone='+00:00')...
And assuming that you want to use the same timezone for all of the rows (i.e. not different timezones based on contents of the row ...
Take the value for "midnight" in the user's specified timezone, and convert that to UTC. e.g. 2016-04-16 00:00 CST6CDT (i.e. America/Chicago) converts to 2016-04-16 05:00 UTC.
Assuming that your table column is named utc_timestamp_col, and is datatype TIMESTAMP, your query would look look something like this:
SELECT ...
FROM mytable t
WHERE t.utc_timestamp_col >= '2016-04-16 05:00' + INTERVAL -1 DAY
AND t.utc_timestamp_col < '2016-04-16 05:00' + INTERVAL 0 DAY
If you have populated the MySQL timezone tables, you can make use of the MySQL support for named timezones. http://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html
You can build an expression that gets you previous "midnight" in a named timezone.
Below is a demonstration query:
verify session time_zone is UTC
return current date and time in UTC
convert to local time zone CST6CDT
truncate to midnight
convert back to UTC
e.g.
SELECT ##session.time_zone AS `time_zone`
, NOW() AS `now_utc`
, CONVERT_TZ(NOW(),'UTC','CST6CDT') AS `now_CST6CDT`
, DATE(CONVERT_TZ(NOW(),'UTC','CST6CDT')) AS `midnight_CST6CDT`
, CONVERT_TZ(DATE(CONVERT_TZ(NOW(),'UTC','CST6CDT')),'CST6CDT','UTC')
AS midnight_CST6CDT_utc
Returns:
time_zone now_utc now_CST6CDT midnight_CST6CDT midnight_CST6CDT_utc
--------- ------------------- ------------------- ---------------- --------------------
UTC 2016-04-17 01:53:31 2016-04-16 20:53:31 2016-04-16 2016-04-16 05:00:00
Demonstrating the same thing, using time zone named 'America/Chicago'
SELECT ##session.time_zone AS `time_zone`
, NOW() AS now_utc
, CONVERT_TZ(NOW(),'UTC','America/Chicago') AS `now_America/Chicago`
, DATE(CONVERT_TZ(NOW(),'UTC','America/Chicago')) AS `midnight_America/Chicago`
, CONVERT_TZ(DATE(CONVERT_TZ(NOW(),'UTC','America/Chicago')),'America/Chicago','UTC')
AS `midnight_America/Chicago_utc`
returns the same result:
time_zone now_utc now_America/Chicago midnight_America/Chicago midnight_America/Chicago_utc
--------- ------------------- ------------------- ------------------------ ----------------------------
UTC 2016-04-17 01:57:19 2016-04-16 20:57:19 2016-04-16 2016-04-16 05:00:00
FOLLOWUP
If I needed to do that kind of timezone conversion in the query, I would use an inline view to return the value from that fairly complicated expression, to make the outer statement simpler. For example, the inline view aliased as "d" returns the value as column named "dt", which can be referenced in the outer query.
Since that inline view returns exactly one row, we can use a JOIN to mytable without duplicating any rows. We can move the predicates from the WHERE clause to an ON clause. e.g.
SELECT ...
FROM ( SELECT CONVERT_TZ(DATE(CONVERT_TZ(NOW(),'UTC','CST6CDT')),'CST6CDT','UTC') AS dt
) d
JOIN mytable t
ON t.utc_timestamp_col >= d.dt + INTERVAL -1 DAY
AND t.utc_timestamp_col < d.dt + INTERVAL 0 DAY
I am new developer in .net,I Have requirement..like this ,when user pick date from date picker not time only date he/she pick up,then click insert that time ,i want insert that date and time is into Column exist with name "EnterdDate" data type is "DATETIME". by default 00:00:00 is stored in the Time format I don't want to be stroed that values I want store The at the Time insertion MySql Server Time.
ex:user 12/03/2013 ->insert->click presently assume server time is 13:00:00
i want insert This Date value --> 2013-03-12 13:00:00 ok for me.
after 10 min I want insert the another record at time i want 2013-03-12 13:10:00
like server time in place of default time i am needed
*i don't need the DateAndTime like is :2013-03-12 00:00:00 not Ok for me.
please give best answer the above question.**
Use ADDTIME() to add the CURTIME() to the given date literal:
INSERT INTO my_table
(EnteredDate)
VALUES
(ADDTIME(CAST('2013-03-12' AS DATETIME), CURTIME())
One needs to CAST() the literal to a DATETIME value because ADDTIME() does not work with DATE types.
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.