I have a table with a column 'ts_min' having time since epoch in minutes
ts_min
--------
24429000
24428340
24427680
I tried the following query to get date representation of these values
select ts_min, from_unixtime(ts_min*60000) from mytable;
from_unixtime is returning null.
ts_min from_unixtime (ts_min * 60000)
----------------------------------------
24429000 NULL
24428340 NULL
24427680 NULL
What is the correct syntax to pass computed value from a column to from_unixtime
yes, 60 is ok. see sample:
sample
MariaDB [(none)]> select from_unixtime(24429000*60);
+----------------------------+
| from_unixtime(24429000*60) |
+----------------------------+
| 2016-06-12 16:00:00 |
+----------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
You can use both sec_to_time and from_unixtime
select SEC_TO_TIME(ts_min*60) from dual
or
select from_unixtime(ts_min*60) from dual
Related
I've a column in a table (varchar) with dates in this format
2013-09-05T10:10:02Z
How do I convert this into datetime format and save it in another column, using an update query?
You can use the STR_TO_DATE function:
UPDATE table1 SET col2 = STR_TO_DATE(col1,'%Y-%m-%dT%TZ')
Example:
mysql> select STR_TO_DATE('2013-09-05T10:10:02Z','%Y-%m-%dT%TZ');
+----------------------------------------------------+
| STR_TO_DATE('2013-09-05T10:10:02Z','%Y-%m-%dT%TZ') |
+----------------------------------------------------+
| 2013-09-05 10:10:02 |
+----------------------------------------------------+
1 row in set (0.00 sec)
You can also use CAST('2013-09-05T10:10:02Z' AS DATETIME) which does not require a format definition as in STR_TO_DATE().
If you want to take care of the timezone just use this query, and use the mysql timezone
mysql> select CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", ##session.time_zone);
+-------------------------------------------------------------------+
| CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", ##session.time_zone) |
+-------------------------------------------------------------------+
| 2013-09-05 12:10:02 |
+-------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
or any other timezone
mysql> select CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", "+03:00");
+--------------------------------------------------------+
| CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", "+03:00") |
+--------------------------------------------------------+
| 2013-09-05 13:10:02 |
+--------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
I tried using the cast method from above but would get the truncated error as describe in the comments.
You can also use CAST('2013-09-05T10:10:02Z' AS DATETIME) which does not require a format definition as in STR_TO_DATE().
I would consistently get: Error: Truncated incorrect datetime value: '2011-10-02T23:25:42Z'
I fixed it by casting the value to an # variable before using it in my query. Here is an example in a Stored Procedure:
CREATE PROCEDURE `new_procedure`(IN p_date VARCHAR(50), p_text VARCHAR(500))
BEGIN
SET #datestring = CAST(p_date AS DATETIME);
-- used for debugging
SELECT #datestring, p_text;
INSERT INTO testtable(timestamp, text) VALUES(#datestring, p_text);
END
I have a column dateofbirth_string which is a VARCHAR containing dates in format dd/mm/yyyy. I would like to convert these values into format DATE (yyyy-mm-dd) and place in field dateofbirth_date in the same row.
Note, some values in dateofbirth_string may be in a bad format e.g. 10/02/15 or 100215. For these values, they can be ignored and I will enter manually.
Thanks in advance for your help!
STR_TO_DATE is your goal.
SELECT STR_TO_DATE('22/04/2020', '%d/%c/%Y');
Sample
MariaDB [(none)]> SELECT STR_TO_DATE('22/04/2020', '%d/%c/%Y');
+---------------------------------------+
| STR_TO_DATE('22/04/2020', '%d/%c/%Y') |
+---------------------------------------+
| 2020-04-22 |
+---------------------------------------+
1 row in set (0.02 sec)
MariaDB [(none)]>
I am trying to get the date part from a timestamp field.
I used this SQL query:
select timestamp, CAST(timestamp as date) as date from messages
I got the following result:
--------------------------------------------
| timestamp | date |
--------------------------------------------
| 2016-05-15 10:22:54 | 2016-05-16 |
--------------------------------------------
As shown above, the date field produced returns the wrong date 2016-05-16 whereas the original date is 2016-05-15.
How can we resolve this issue?
Thats not a issue !!! Its only set the wrong time_zone. see sample
get current time_zone
SHOW GLOBAL VARIABLES LIKE 'time_zone'; -- systemwide setting
SHOW VARIABLES LIKE 'time_zone'; -- session setting
sample
MariaDB [mysql]> select t, CAST(t as date) FROM groupme LIMIT 1;
+---------------------+-----------------+
| t | CAST(t as date) |
+---------------------+-----------------+
| 2016-05-15 20:22:54 | 2016-05-15 |
+---------------------+-----------------+
1 row in set (0.00 sec)
MariaDB [mysql]> SET time_zone ='-12:00';
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> select t, CAST(t as date) FROM groupme LIMIT 1;
+---------------------+-----------------+
| t | CAST(t as date) |
+---------------------+-----------------+
| 2016-05-14 20:22:54 | 2016-05-14 |
+---------------------+-----------------+
1 row in set (0.00 sec)
MariaDB [mysql]>
Use date not cast because is not casting but a format
select timestamp, date(timestamp) as my_date from messages
I would suggest you to use the DATE_FORMAT function and not the CAST since you are formatting the date like
SELECT `timestamp`, DATE_FORMAT(`timestamp`, '%Y-%m-%d) as my_date from messages
Also note that both CAST and DATE function internally call Item_date_typecast function so there is no such difference between them.
Try this
select timestamp, cast(timestamp as date format 'yyyymmddhhmmss') as date from messages
How to get the current time in millisecond in MySQL.
So far what I've got is UNIX_TIMESTAMP().
But it returns the current time in seconds.
But I want the current time in milliseconds in UTC since '1970-01-01 00:00:00' in MySQL.
Any suggestion?
Looking at the documentation, it sounds like you want
NOW(3)
... where the value 3 is used to specify that you want 3 digits of subsecond precision, i.e. milliseconds. (Unfortunately none of the examples in the docs show that being used, so it's relatively tricky for me to check this...)
To get that as a "milliseconds since the Unix epoch" value, you'd probably want to use:
UNIX_TIMESTAMP(NOW(3)) * 1000
Thi is my answer;
mysql> Select curtime(4);
+------------+
| curtime(4) |
+------------+
| 13:27:20 |
+------------+
1 row in set (0.00 sec)
Or can also make this:
mysql> select conv(
-> concat(
-> substring(uid,16,3),
-> substring(uid,10,4),
-> substring(uid,1,8))
-> ,16,10)
-> div 10000
-> - (141427 * 24 * 60 * 60 * 1000) as current_mills
-> from (select uuid() uid) as alias;
+---------------+
| current_mills |
+---------------+
| 1427995791797 |
+---------------+
1 row in set (0.00 sec)
I've a column in a table (varchar) with dates in this format
2013-09-05T10:10:02Z
How do I convert this into datetime format and save it in another column, using an update query?
You can use the STR_TO_DATE function:
UPDATE table1 SET col2 = STR_TO_DATE(col1,'%Y-%m-%dT%TZ')
Example:
mysql> select STR_TO_DATE('2013-09-05T10:10:02Z','%Y-%m-%dT%TZ');
+----------------------------------------------------+
| STR_TO_DATE('2013-09-05T10:10:02Z','%Y-%m-%dT%TZ') |
+----------------------------------------------------+
| 2013-09-05 10:10:02 |
+----------------------------------------------------+
1 row in set (0.00 sec)
You can also use CAST('2013-09-05T10:10:02Z' AS DATETIME) which does not require a format definition as in STR_TO_DATE().
If you want to take care of the timezone just use this query, and use the mysql timezone
mysql> select CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", ##session.time_zone);
+-------------------------------------------------------------------+
| CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", ##session.time_zone) |
+-------------------------------------------------------------------+
| 2013-09-05 12:10:02 |
+-------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
or any other timezone
mysql> select CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", "+03:00");
+--------------------------------------------------------+
| CONVERT_TZ("2013-09-05T10:10:02Z", "+00:00", "+03:00") |
+--------------------------------------------------------+
| 2013-09-05 13:10:02 |
+--------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
I tried using the cast method from above but would get the truncated error as describe in the comments.
You can also use CAST('2013-09-05T10:10:02Z' AS DATETIME) which does not require a format definition as in STR_TO_DATE().
I would consistently get: Error: Truncated incorrect datetime value: '2011-10-02T23:25:42Z'
I fixed it by casting the value to an # variable before using it in my query. Here is an example in a Stored Procedure:
CREATE PROCEDURE `new_procedure`(IN p_date VARCHAR(50), p_text VARCHAR(500))
BEGIN
SET #datestring = CAST(p_date AS DATETIME);
-- used for debugging
SELECT #datestring, p_text;
INSERT INTO testtable(timestamp, text) VALUES(#datestring, p_text);
END