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)]>
Related
The actor table in the salika sample schema defines column last_update as a timestamp. I want to render that column using JSON_ARRAY in ISO8601 format. - First shouldn't that be the default rendering for JSON_ARRAY.
From reading the documentation and comments on this website and others it appears that the answer is to use FROM_UNXTIME with an output mask that generates ISO8601 Format.
Unfortunately FROM_UNIXTIME() appears to always return NULL on my database
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2018-10-03 17:15:03 |
+---------------------+
1 row in set (0.00 sec)
mysql> select from_unixTime(current_timestamp())
-> ;
+------------------------------------+
| from_unixTime(current_timestamp()) |
+------------------------------------+
| NULL |
+------------------------------------+
1 row in set (0.00 sec)
mysql>
I suspect this may be caused by the fact that I have not installed the timezone configuration files.. However when I try that I get...
mysql -u root -p****** sys <timezone_posix.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'sys.time_zone' doesn't exist
Sure I've missed something obvious here....
Just try this:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_TIMESTAMP()));
As mysql doc says, function FROM_UNIXTIME(unix_timestamp[,format]) only accept parameter as a UNIX_TIMESTAMP, but not a TIMESTAMP.
Luckily, there's a function UNIX_TIMESTAMP(date) transforming various types such as DATE, TIMESTAMP and so on into UNIX_TIMESTAMP. So call UNIX_TIMESTAMP(date) first, and then `FROM_UNIXTIME(
This worked for me in the end...
select json_array("actor_id","first_name","last_name",DATE_FORMAT(convert_tz("last_update", ##session.time_zone, '+00:00'),'%Y-%m-%dT%T%fZ')) "json" from "sakila"."actor"
/
which gives
[
1,
"PENELOPE",
"GUINESS",
"2006-02-15T12:34:33000000Z"
],
[
2,
"NICK",
"WAHLBERG",
"2006-02-15T12:34:33000000Z"
]
I am trying to convert a TIMESTAMP to the DATETIME, DATE and TIME MySQL types using the FROM_UNIXTIME field. I noticed that the values for TIME and DATETIME fields have different time information on them.
Instead of the insert statement to the table, I am replacing it with a simple select statement that prints the values:
select FROM_UNIXTIME('1468561341') as timestamp_datetime,FROM_UNIXTIME('1468561341','%d/%m/%y') as timestamp_date, FROM_UNIXTIME('1468561341','%h:%m:%s %p') as timestamp_time
The results look like this:
timestamp_datetime: 2016-07-15 05:42:21.000000
timestamp_date 15/07/16
timestamp_time 05:07:21 AM
How can I get consistent time value in both these columns?
the issue is with letter m
it should be i
mysql> select FROM_UNIXTIME(1468561341) as timestamp_datetime,FROM_UNIXTIME(1468561341,'%d/%m/%y') as timestamp_date, FROM_UNIXTIME(1468561341,'%h:%i:%s %p') as timestamp_time;
+---------------------+----------------+----------------+
| timestamp_datetime | timestamp_date | timestamp_time |
+---------------------+----------------+----------------+
| 2016-07-15 05:42:21 | 15/07/16 | 05:42:21 AM |
+---------------------+----------------+----------------+
1 row in set (0.05 sec)
Your problem is that in your FROM_UNIXTIME you are essentially telling it to do
hours-months-seconds -AM/PM
But you want
hours-minutes-seconds -AM/PM
So change it to
FROM_UNIXTIME(1468561341,'%h:%i:%s %p')
instead.
Reference guide for all the time parameters and usage of time functions in MySQL here: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
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
Im newbie for sql. My code:
$start_date='2017-01-01';
$end_date='2016-01-31';
$this->db->where('date_note BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
But in my database is like 2016-03-11 14:21:36
How I can get this record from database with different format date? Thanks
The MySQL function DATE_FORMAT() resolves fully your problem as it allows you to get the time you saved within your database in the format of your choice.
There is no problem in your SQL. It is okay to compare DATE with DATATIME(as in your database).
Here is a demo:
mysql> SELECT '2016-03-11 14:21:36' BETWEEN '2016-03-10' AND '2016-03-12';
+-------------------------------------------------------------+
| '2016-03-11 14:21:36' BETWEEN '2016-03-10' AND '2016-03-12' |
+-------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
quick question. In the DATETIME column in a MYSQL table, the format is, YYYY-MM-DD. So If i put insert today's date, as 2014-01-09, MYSQL will understand... But will it understand if i insert data like this?
YYYY/MM/DD or 2014/01/09
From the manual:
MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts.
So it would seem the answer is, yes.
mysql> select date('2016/06/16');
+--------------------+
| date('2016/06/16') |
+--------------------+
| 2016-06-16 |
+--------------------+
1 row in set (0.01 sec)