I am working with a mysql database that somebody else created. I have a table with two different date fields one is called eventDate and is in the format YYYY-MM-DD and the other is called creationDate and is in the format M/D/YYYY H:MM:SS AM/PM
What I want to do is query the table and return results where the eventDate is the same as the creationDate but how do I convert the creationDate into YYYY-MM-DD format?
Try STR_TO_DATE() and a format matching the one used by your creationDate strings:
SELECT
…
WHERE DATE(STR_TO_DATE(creationDate, '%c/%e/%Y %r')) = STR_TO_DATE(eventDate, '%Y-%m-%d')
Perhaps MySQL would be able to convert YYYY-MM-DD strings to dates implicitly, so the second STR_TO_DATE() call might be unnecessary.
Related
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 want to fetch all datas that corresponds in the chosen date range.
So the problem is that. When theres included time in the data. It can't fetch the required data to be displayed. But when I remove the time on it. It displays really well. What can I do to make it right?
EXAMPLE VALUES:
2018-10-29 01:21:29pm
2018-10-30 01:21:29pm
EXAMPLE VALUES THAT WORKS:
2018-10-29
2018-10-30
My query:
`"SELECT *,SUBSTRING(order_date,1,10) from orders where order_date >='$fromdate' AND order_date <='$todate'"`
Ideal Solution: You will need to change the datatype of order_date from Varchar(500) to Datetime type, using Alter Table command.
Now, it is noteworthy that the MySQL datetime value is in YYYY-MM-DD HH:MM:SS format. So firstly, you will need to change your datetime string to MySQL datetime format string. Otherwise, directly changing the datatype will lead to irreparable loss/truncation of data.
Your datetime value 2018-10-29 01:21:29pm is basically of YYYY-MM-DD HH:MM:SS AM/PM (12 hour format). In terms of format specifiers, it would be: '%Y-%m-%d %h:%i:%s%p'. Complete list of available format specifiers can be seen in MySQL docs.
Firstly, we use Str_To_Date() function to convert all your data into proper Datetime format.
UPDATE orders
SET order_date = STR_TO_DATE(order_date, '%Y-%m-%d %h:%i:%s%p');
Now, next step is simple. Just modify the datatype to datetime:
ALTER TABLE orders
MODIFY COLUMN order_date datetime;
Is there any way to convert a normal european date to a timestamp-value in mySQL?
I have a mysql database with a column date (varchar(64)) containing dates in european format like 01.12.15. For this I need a update-sql-query that converts all lines into timestamps.
Thanks for your help!
Use STR_TO_DATE to convert your date string 26.11.17 to a bona fide date. Then call UNIX_TIMESTAMP on that date to get the UNIX timestamp.
SELECT
UNIX_TIMESTAMP(STR_TO_DATE('26.11.17', '%d.%m.%y')) AS output
FROM dual;
1511650800
Demo
I have stored the dates as string in my database.I know it is not good,but project already has been half developed before i take over and where dates were stored as string,so i was continuing the same way.
Now i want to select dates from table where date is greater than a specific date.
I tried the following query
SELECT
*
FROM
dates
where
STR_TO_DATE(date, '%Y-%m-%d') > "2014-01-01"
but it is not returning only greater values.
Please help me to solve problem.
Demo
Your dates are not in YYYY-MM-DD format. Use the right format!
SELECT *
FROM dates
where STR_TO_DATE(date, '%m-%d-%Y') > date('2014-01-01')
If you are going to store dates as strings, then the best way is in the ISO format of YYYY-MM-DD.
You should read the documentation on str_to_date() (here).
Convert everything to date and it should be fine. Now you are comparing date and string.
What type has the date? I'd prefer a ' instead of " for strings in SQL. Let's assume that date is a VARCHAR or TEXT field (depending on which database you are using):
SELECT *
FROM dates
WHERE STR_TO_DATE(date, '%Y-%m-%d') > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
If date is a real DATE
SELECT *
FROM dates
WHERE trim(date) > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
Or you just convert it into a number format date_format(date,'%Y%m%d') > 20140101
I am trying to store the date in mysql as mm-dd-yyyy.
The following query updates the table stores the date as 0000-00-00
UPDATE `h3`.`newbatch` SET `DateCreated` = '11-08-2013' WHERE
`newbatch`.`BatchID` =
1 AND `newbatch`.`DateCreated` = '2013-11-08' LIMIT 1
I can always use DATE_FORMAT(DateCreated,'%m %d %Y') during select but is there a way to store date in that format.
The datatype of DateCreated is Date.
I am using MySQL.
Thanks
Do not modify the storage format of a date. The format for the date data type is ISO 8601 standard for a reason. You will lose the ability to perform most date functions elegantly (without first converting to the standard date format). You do the formatting when you run a query.