I'm trying to get a datetime column from my table using a simple SELECT statement, but I want the datetime without seconds.
The datetime column is formatted as 10/21/2013 3:19:33 PM.
My query is SELECT DATE_FORMAT(creationdate, '%m-%d%Y %l:%i %p') AS 'Creation Date' FROM processes;
When I run this query, it returns all NULL values. I've double and triple checked, and the column cells do hold datetime values.
However, when I run SELECT DATE_FORMAT(NOW(), '%m-%d%Y %l:%i %p) AS 'Creation Date' FROM processes; I get the current date with correct formatting.
What could cause this?
creationdate must not be of type datetime if it is formatted 10/21/2013 3:19:33 PM by default.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format.
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
Related
I have a column of dates written as strings with "am" and "pm" at the end, for example:
1/1/2016 12:00:00.000 AM
The type of the column is currently varchar, I want to change the type to datetime format.
Attempt 1
Changing the column type from phpmyadmin dashboard gives the following error:
Query error:
#1292 - Incorrect datetime value: '1/1/2016 12:00:00.000 AM'
Attempt 2
Considering the format of the date, I tried to use the STR_TO_DATE function. But it does not give the expected result. It instead returns the same date for each row.
SELECT STR_TO_DATE(`assembling-machine-tag`.`Time`, "%Y") from `assembling-machine-tag`
Result
So, how can I change the format of my column from varchar (with format = dd/mm/yy hh:mm:ss.ms AM) to datetime (with format = yyyy-mm-dd hh:mm:ss) in MySQL 7.4.1?
You just need to use the correct pattern:
SELECT STR_TO_DATE('1/2/2016 01:02:03.456 AM', '%e/%c/%Y %h:%i:%s.%f %p')
-- 2016-02-01 01:02:03.456000
In order to convert varchar data to datetime, you first need to create a temporary datetime column and update it like so:
UPDATE t SET `datetimetemp` = STR_TO_DATE(`varchardate`, '%c/%e/%Y %h:%i:%s.%f %p')
Once satisfied with result drop, the varchar column and rename the temporary column.
I would use this version:
SELECT STR_TO_DATE('1/1/2016 12:00:00.000 PM', '%d/%m/%Y %h:%i:%s.%f %p')
Here is my table "tb_posts":
I want to select only those rows where datetime field i.e. post_date_published is not equal to 0000-00-00 00:00:00. I am using following query but it doesn't work:
SELECT * FROM `tb_posts` WHERE `post_date_published` IS NOT NULL
I am getting the same output as shown in the above picture.
Why IS NOT NULL is not working?
As per the MYSQL documentation it saves invalid dates as '0000-00-00 00:00:00'. It will not be considered as NULL.
Try comparing with the date '0000-00-00 00:00:00':
SELECT * FROM tb_posts where post_date_published != '0000-00-00 00:00:00'
A method I use with this sort of thing is
SELECT `columns` FROM `tb_posts` WHERE UNIX_TIMESTAMP(`post_date_published`) > 0
From the MySQL Documentation:
The valid range of argument values is the same as for the TIMESTAMP
data type: '1970-01-01 00:00:01.000000' UTC to '2038-01-19
03:14:07.999999' UTC. If you pass an out-of-range date to
UNIX_TIMESTAMP(), it returns 0.
The UNIX_TIMESTAMP function forces the result to be an integer so it's much easier to work with in these quick comparisons. It is also vital for working with MySQL 5.7 where "empty" (ie zero value) date/time columns are not allowed.
(I had a lot of grief trying to convert various date columns to NULL because MySQL 5.7+ didn't recognise 0000-00-00 00:00:00 as a valid comparison -- so I converted it to a unix timestamp so as to compare the timestamp rather than the actual [invalid] date.)
The default date format in my SQL is : '2019-05-06'
and its fine but when i insert date to my table i want this format 2019-5-6 not the above format
It means month and day must be start 1 to 30 not 01 to 31.Is there any way to change default format in my sql?
You seem to be looking for the MySQL STR_TO_DATE function :
It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
So if the date coming out of your application is like '2019-5-6', to convert it to a MySQL date you need :
STR_TO_DATE('2019-5-6', '%Y-%c-%e')
In an INSERT statement :
INSERT INTO mytable VALUES(1, STR_TO_DATE('2019-5-6', '%Y-%c-%e'));
Tip :
%Y : Year as a numeric, 4-digit value
%c : numeric month name (0 to 12)
%e: day of the month as a numeric value (0 to 31)
The default way to store a date in a MySQL database is by using DATE. The proper format of a DATE is: YYYY-MM-DD. If you try to enter a date in a format other than the Year-Month-Day format, it might work but it won't be storing the dates as you expect.
In order to run a MySQL Insert command and add the current date into your table you can use MySQL's built-in function CURDATE() in your query.
An example of how to Insert a Date in MySQL using CURDATE
$query_auto = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )";
Also, you can run a query to set the date manually
An example of how to Insert a Date in MySQL manually
$query_manual = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')";
It is recommended to do the date formatting when doing a query, like so:
SELECT DATE_FORMAT(BirthDate, "%W %M %e %Y") FROM Employees;
You can find more examples of formatting the date here:
https://www.w3schools.com/sql/func_mysql_date_format.asp
I have a datetime datatype in my table,
`DateAdded` datetime(4) DEFAULT NULL,
I have a record in my database with DateAdded = 2017-09-11 17:02:48.6531 value, ( it's ID = 16452994 ).
When I want to get it with following query return NULL
select `ID`,`DateAdded` from `Add` where `DateAdded` <= FROM_UNIXTIME(('1505071799' +86400 ), '%Y-%m-%d %h:%i:%s') and ID =16452994 ;
FYI : FROM_UNIXTIME(('1505071799' +86400 ), '%Y-%m-%d %h:%i:%s') = 2017-09-11 23:59:59
It's strange that 2017-09-11 17:02:48.6531 <= 2017-09-11 23:59:59 return false
but when I try the following query I'll get my desire result.
select `ID`,`DateAdded` from `Add` where `DateAdded` <= FROM_UNIXTIME(('1505071799' +86400 +1 ), '%Y-%m-%d %h:%i:%s') and ID =1645299;
I want to know why this is happening and how can I resolve this problem?
FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format)
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone. unix_timestamp is an internal timestamp value such as is produced by the UNIX_TIMESTAMP() function.
If format is given, the result is formatted according to the format string, which is used the same way as listed in the entry for the DATE_FORMAT() function.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
I am trying to convert dates in the format mm/dd/yyyy to the standard date format yyyy-mm-dd using the STR_TO_DATE function. Some fields in my date column are null and some contain a date.
For instance, 8/22/2011 should become 2011-8-22.
When I select my date column, it looks like this:
8/22/2011
8/10/2010
5/12/2012
etc.
I tried using the code
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
which filled the column with NULL values. Also tried
UPDATE table SET date = STR_TO_DATE(#date, '%m/%d/%Y')
with same result, although this time I did not get a warning message.
The first one is correct:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
But if the date is not valid (not in %m/%d/%Y format) then it returns NULL
Try executing and then showing warnings. It tells you what is wrong:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y');
SHOW WARNINGS;
Maybe some dates are not in format %m/%d/%Y (posibly %d/%m/%Y)