DATE_FORMAT not picking my format in mysql - mysql

this is my query (Not Working): SELECT date_format('21-Oct-19 15:59','%Y-%m-%d %H:%i') + interval 3 day, date_format(now(), '%d-%b-%y %H:%i')
this is my query (Working): SELECT '2019-10-21 15:59' + interval 3 day, date_format(now(), '%d-%b-%y %H:%i')
My data is in this format 21-Oct-19 15:59 i want to add 3 day in it. How I can achieve this?

If you have a datetime in a non-standard format (that is, non YYYY-MM-DD HH:MM:SS), you need to convert it to a standard datetime using STR_TO_DATE().
You used DATE_FORMAT() which does the opposite — it only accepts a standard datetime, and you can format a non-standard datetime.
You also need a different format specifier corresponding to the non-standard datetime input. You don't need to specify the output format for STR_TO_DATE(). It always outputs standard datetime.
mysql> SELECT STR_TO_DATE('21-Oct-19 15:59','%y-%b-%d %H:%i') AS d;
+---------------------+
| d |
+---------------------+
| 2021-10-19 15:59:00 |
+---------------------+
mysql> SELECT STR_TO_DATE('21-Oct-19 15:59','%y-%b-%d %H:%i') + INTERVAL 3 DAY AS d;
+---------------------+
| d |
+---------------------+
| 2021-10-22 15:59:00 |
+---------------------+

You need to do the date_format after adding the interval :
SELECT date_format(now() + interval 3 day, '%d-%b-%y %H:%i') ;

Related

Formating int datetime in codeigniter mysql

I have Datetime in integer in database.
Example. 1597192194
How can convert that in to date format dd-mm-yy h:i:s.
I have tried this one, not it's not working.
$this->db->select("DATE_FORMAT(created_on, '%Y-%m-%d %h:%i %p') as registered_on");
Please help.
Consider:
date_format(from_unixtime(created_on), '%d-%m-%Y %H:%i:%s') as registered_on
Rationale:
from_unixtime() converts your epoch timestamp to a datetime value
then date_format() applies the target format
Demo on DB Fiddlde:
select date_format(from_unixtime(1597192194), '%d-%m-%Y %H:%i:%s') as registered_on
| registered_on |
| :------------------ |
| 12-08-2020 01:29:54 |

How can I convert MySQL date stored as negative (signed) bigint to date format?

I have birth dates stored in a MySQL database that I need converted to a readable date format yyyy-mm-dd hh:mm:ss. I cannot use the MySQL's From_Unix function as many birth dates are before 1970 and the function returns NULL. (i.e. -866138400000 which is 07/21/1942)
I have tried to use ticks but that is also returning NULL:
(FROM_DAYS(365 + (req.PATIENTDOB / 864000000000)) + INTERVAL (req.PATIENTDOB % 864000000000) / 10000000 SECOND) AS ptDob
Any advance would be greatly appreciated. Thank you.
I have no idea why you're making things so complicated. Just divide by 1000 to get seconds instead of microseconds and subtract that from 1970-01-01.
mysql > select '1970-01-01' + interval -866138400000/1000 second;
+---------------------------------------------------+
| '1970-01-01' + interval -866138400000/1000 second |
+---------------------------------------------------+
| 1942-07-22 06:00:00 |
+---------------------------------------------------+
1 row in set (0.00 sec)
So your query would actually be this of course:
select '1970-01-01' + interval your_column / 1000 second from your_table;
This query proves, that your assumption, that it would be 1942-07-21 is wrong. 1942-07-22 is correct.
mysql > select timestampdiff(second, '1942-07-21', '1970-01-01');
+---------------------------------------------------+
| timestampdiff(second, '1942-07-21', '1970-01-01') |
+---------------------------------------------------+
| 866246400 |
+---------------------------------------------------+
Found an answer while researching Negative Epochs. I was able to use the From_Unixtime function after all!
select date_format((DATE_ADD(FROM_UNIXTIME(0), interval -866138400000/ 1000 second)),'%Y-%m-%d') as ptdate;
-> "1942-07-21"
Link to Reference > Look under Negative Epochs section

Convert datetime column to 24 hour format

I have a column called schedule_time (datetime) format. I want to convert the time to 24 hour time.
2016-03-08 03:00:00 to 2016-03-08 15:00:00
Please note that DATETIME values are always stored in 24h format (http://dev.mysql.com/doc/refman/5.7/en/datetime.html). There is no AM/PM.
When you want to display the values, there is however the DATE_FORMAT
function, which will format the value according to your needs, including AM/PM:
select DATE_FORMAT(schedule_time, '%Y-%m-%d %h:%i:%s %p') from t1;
This will give 2016-03-08 03:00:00 AM and 2016-03-08 03:00:00 PM. But the values in the DB are still the same, in 24h format.
If adding 12 hours would solve your issue, the you can do it like this:
start transaction;
update t1 set schedule_time = date_add(schedule_time, interval 12 hour);
select * from t1; -- verify!!!
rollback;
-- or commit;
I put this in a transaction so you can first verify your results. If they are wrong, simply rollback the transaction (provided you use InnoDB tables). If you don't have transactions (or feel uncomfortable with them), you can undo the change with date_sub instead of date_add.
But be aware: This doesn't change from 12h to 24h format, it simply adds 12 hours to all your schedule_time values.
Use MySQL's DATE_FORMAT function.
The format string will be '%Y-%m-%d %T'.
Selecting the current date with 24-hour time:
mysql> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %T') AS now
+---------------------+
| now |
+---------------------+
| 2016-03-08 20:47:04 |
+---------------------+
1 row in set (0.00 sec)
Selecting a date with 24-hour time from a table:
mysql> SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d %T') AS created_at FROM test.comments;
+---------------------+
| created_at |
+---------------------+
| 2016-02-25 16:32:12 |
+---------------------+
1 row in set (0.00 sec)

How to convert UNIX time before 1970 to date format in MySQL?

I have a database using unix time for its dates ( i am using mySQL). I want to retrieve the dates in daily date format. This is my query:
SELECT FROM_UNIXTIME(time_created) FROM member
This works fine with dates after 1970 (for example, 1314162229) but doesn't work for dates before 1970 (for example, -769338000). Is there any work around here?
A possible workaround would be to have a constant handy corresponding to the seconds in a certain number of years (preferrably a multiple of 4). You could add this constant, translate the time and then subtract the number of years chosen.
Example: choose 40 years.
Determine the constant:
MySQL [files]> select adddate(from_unixtime(0), interval 40 year);
+---------------------------------------------+
| adddate(from_unixtime(0), interval 40 year) |
+---------------------------------------------+
| 2010-01-01 01:00:00 |
+---------------------------------------------+
1 row in set (0.09 sec)
MySQL [files]> select unix_timestamp(adddate(from_unixtime(0), interval 40 year));
+-------------------------------------------------------------+
| unix_timestamp(adddate(from_unixtime(0), interval 40 year)) |
+-------------------------------------------------------------+
| 1262304000 |
+-------------------------------------------------------------+
1 row in set (0.09 sec)
Now you can every unix timestamp x between 1930 and 20xx and use it.
select subdate(from_unixtime(x+1262304000), interval 40 year);
With your example -769338000, you get
MySQL [files]> select subdate(from_unixtime(-769338000+1262304000), interval 40 year);
+-----------------------------------------------------------------+
| subdate(from_unixtime(-769338000+1262304000), interval 40 year) |
+-----------------------------------------------------------------+
| 1945-08-15 17:00:00 |
+-----------------------------------------------------------------+
1 row in set (0.09 sec)
I found a new way:
converting to MySQL date:
SELECT DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second);
converting your epoch to a date string:
SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second), '%Y-%m-%d');
And back
SELECT TIMESTAMPDIFF(second,FROM_UNIXTIME(0),'1960-01-01 00:00:00' );
source:
http://www.epochconverter.com/programming/mysql-from-unixtime.php#negavtiveEpoch
SELECT DATE_ADD(CAST('1970-01-01 00:00:00' AS DATETIME), INTERVAL `time_created` SECOND) FROM `member`
To my knowledge there is no such thing as UNIX time prior to 1/1/1970 00:00 UTC. More at Wikipedia.

How to select date from datetime column?

I have a column of type "datetime" with values like 2009-10-20 10:00:00
I would like to extract date from datetime and write a query like:
SELECT * FROM
data
WHERE datetime = '2009-10-20'
ORDER BY datetime DESC
Is the following the best way to do it?
SELECT * FROM
data
WHERE datetime BETWEEN('2009-10-20 00:00:00' AND '2009-10-20 23:59:59')
ORDER BY datetime DESC
This however returns an empty resultset. Any suggestions?
You can use MySQL's DATE() function:
WHERE DATE(datetime) = '2009-10-20'
You could also try this:
WHERE datetime LIKE '2009-10-20%'
See this answer for info on the performance implications of using LIKE.
Using WHERE DATE(datetime) = '2009-10-20' has performance issues. As stated here:
it will calculate DATE() for all rows, including those that don't match.
it will make it impossible to use an index for the query.
Use BETWEEN or >, <, = operators which allow to use an index:
SELECT * FROM data
WHERE datetime BETWEEN '2009-10-20 00:00:00' AND '2009-10-20 23:59:59'
Update: the impact on using LIKE instead of operators in an indexed column is high. These are some test results on a table with 1,176,000 rows:
using datetime LIKE '2009-10-20%' => 2931ms
using datetime >= '2009-10-20 00:00:00' AND datetime <= '2009-10-20 23:59:59' => 168ms
When doing a second call over the same query the difference is even higher: 2984ms vs 7ms (yes, just 7 milliseconds!). I found this while rewriting some old code on a project using Hibernate.
You can format the datetime to the Y-M-D portion:
DATE_FORMAT(datetime, '%Y-%m-%d')
Though all the answers on the page will return the desired result, they all have performance issues. Never perform transformations on fields in the WHERE clause (including a DATE() calculation) as that transformation must be performed on all rows in the table.
The BETWEEN ... AND construct is inclusive for both border conditions, requiring one to specify the 23:59:59 syntax on the end date which itself has other issues (microsecond transactions, which I believe MySQL did not support in 2009 when the question was asked).
The proper way to query a MySQL timestamp field for a particular day is to check for Greater-Than-Equals against the desired date, and Less-Than for the day after, with no hour specified.
WHERE datetime>='2009-10-20' AND datetime<'2009-10-21'
This is the fastest-performing, lowest-memory, least-resource intensive method, and additionally supports all MySQL features and corner-cases such as sub-second timestamp precision. Additionally, it is future proof.
Here are all formats
Say this is the column that contains the datetime value, table data.
+--------------------+
| date_created |
+--------------------+
| 2018-06-02 15:50:30|
+--------------------+
mysql> select DATE(date_created) from data;
+--------------------+
| DATE(date_created) |
+--------------------+
| 2018-06-02 |
+--------------------+
mysql> select YEAR(date_created) from data;
+--------------------+
| YEAR(date_created) |
+--------------------+
| 2018 |
+--------------------+
mysql> select MONTH(date_created) from data;
+---------------------+
| MONTH(date_created) |
+---------------------+
| 6 |
+---------------------+
mysql> select DAY(date_created) from data;
+-------------------+
| DAY(date_created) |
+-------------------+
| 2 |
+-------------------+
mysql> select HOUR(date_created) from data;
+--------------------+
| HOUR(date_created) |
+--------------------+
| 15 |
+--------------------+
mysql> select MINUTE(date_created) from data;
+----------------------+
| MINUTE(date_created) |
+----------------------+
| 50 |
+----------------------+
mysql> select SECOND(date_created) from data;
+----------------------+
| SECOND(date_created) |
+----------------------+
| 31 |
+----------------------+
You can use:
DATEDIFF ( day , startdate , enddate ) = 0
Or:
DATEPART( day, startdate ) = DATEPART(day, enddate)
AND
DATEPART( month, startdate ) = DATEPART(month, enddate)
AND
DATEPART( year, startdate ) = DATEPART(year, enddate)
Or:
CONVERT(DATETIME,CONVERT(VARCHAR(12), startdate, 105)) = CONVERT(DATETIME,CONVERT(VARCHAR(12), enddate, 105))
simple and best way to use date function
example
SELECT * FROM
data
WHERE date(datetime) = '2009-10-20'
OR
SELECT * FROM
data
WHERE date(datetime ) >= '2009-10-20' && date(datetime ) <= '2009-10-20'
I tried date(tscreated) = '2022-06-04' on a large record set. My tscreated is indexed. It took 42 seconds.
I then tried tscreated >= '2022-06-04' and tscreated < '2022-06-05' and the time was .094 sec.
I realize that the record set might be in memory the second time, but I also believe that the date function negates the value of the index.
Well, using LIKE in statement is the best option
WHERE datetime LIKE '2009-10-20%'
it should work in this case