mySql: convert date from Jan 01, 2000 to 2000-01-01 - mysql

I am trying to 'normalize' some data from a column that has two date formats, one like "Mon dd, yyyy and another "YYYY-mm-dd". I would like to convert all of the first into the second format, and then change the column type to date.
I imagine it is something like this:
UPDATE table SET
`thedate` = DATE_FORMAT(`thedate`, '%Y-%m-%d')
WHERE `thedate` LIKE '%,%'
but the DATE_FORMAT is the wrong function, I think.
Any ideas?
thanks.

I think I got it.
SELECT date_format( str_to_date( thedate, '%M %d, %Y' ) , '%Y-%m-%d' )
newdate, thedate
FROM donor
WHERE `thedate` LIKE '%,%'

Related

SQL get dates later than specified date

I'm working on a database that stores a date column in a human-readable format. This seems to make it tricky to work out rows where a given date is later than.
The format is stored like 20 November, 2018
I'm trying to return all rows where the collection_date is later than 5 November, 2018
I've tried the following query which throws an error.
SELECT *
FROM `orders`
WHERE collection_date >= CONVERT(datetime, '20181105')
This throws the following error message:
Here's my DB info:
UPDATE 1:
I'm trying the following query. None of the previous queries have worked so far, all producing 0 rows:
SELECT *
FROM `orders`
WHERE STR_TO_DATE('collection_date', '%d %M, %Y') >= 2018-11-05
This also does not work
Actually... you have to apply STR_TO_DATE on the collection_date column because that is where the human readable dates are. As for the input date, just specify it in yyyy-mm-dd format:
SELECT *
FROM `orders`
WHERE STR_TO_DATE(collection_date, '%e %M, %Y') >= '2018-11-05'
In MySQL, you want STR_TO_DATE():
SELECT o.*
FROM `orders` o
WHERE collection_date >= Str_To_Date(datetime, '%e %M, %Y');
CONVERT() with this syntax is a SQL Server function.
The value comes first in the convert() function
WHERE collection_date >= CONVERT('2018-11-05', date)
Demo
You can try this using STR_TO_DATE() function
SELECT *
FROM `orders`
WHERE collection_date >= STR_TO_DATE('20181105', '%Y%m%d')
This query worked for me:
SELECT *
FROM orders
WHERE STR_TO_DATE(collection_date, '%d %M, %Y') >= '2018-11-05'
AND collection_time = '4:00 PM'
ORDER BY `orders`.`collection_date` DESC

mysql: change datetime format when select * from table

I need to query and fetch a row in a table and change the default datetime format 2015-09-15 00:00:00 to simply Sep 02 2015. Any idea how to achieve this?
I tried
SELECT
*
from tablename
where id=0;
SELECT
DATE_FORMAT(date, '%b %d %Y')
FROM tablename.
It returns two tables.
This should work:
select sub.comments_id, sub.comment, date_format(sub.date, '%b %d %Y'), sub.views
from (select comments_id, comment, date, views from tablename where id=0) as sub;
The power of MySQL is in sub selection, take advantage of it.
Use the following query:
select date_format(str_to_date(date, '%Y-%m-%d %H:%i:%s'), '%b %d %Y') from tablename;
str_to_date converts string representation to date and date_format gets you the required formatted string.

Mysql: Change date format

I want to change the date format in mm/yyyy. My query like that.
select shipwynum,
IF (s.adjdeldat != '', s.adjdeldat,s.condeldat) as -- adjdeldat and condeldat are date type
deliverydate,
FROM ship
Result of that query come in yyyymmdd fromat but i want result in mm/yyyy format.
I use DATE_FORMAT like that
DATE_FORMAT(s.adjdeldat,'%m/%Y')
DATE_FORMAT(s.condeldat,'%m/%Y')
but it does not work properly.
From the comments and post:
Data type is varchar for adjdeldat and condeldat and it save data in yyyymmdd or yyyymm
I want to change the date format in mm/yyyy
You mean some data is in 20140415 format and some in 201404 format?
Yes, and i want that data in 04/2014 format
Change your query as below:
select
shipwynum,
if( s.adjdeldat != '',
date_format( str_to_date( s.adjdeldat, '%Y%m%d' ), '%m/%Y' ),
date_format( str_to_date( s.condeldat, '%Y%m%d' ), '%m/%Y' )
) deliverydate,
from ship
Even if some the date values miss dd part, MySQL silently replaces them with 00 when converted from str_to_date.
Example:
select
#dt:=str_to_date( '201404', '%Y%m%d' ) dt,
date_format( #dt, '%m/%Y' ) df;
Result:
+------------+---------+
| dt | df |
+------------+---------+
| 2014-04-00 | 04/2014 |
+------------+---------+
1 row in set (0.00 sec)
try this:
DATE_FORMAT(NOW(),'%m-%d-%Y')
http://www.w3schools.com/sql/func_date_format.asp
your code as like:
SELECT DATE_FORMAT(NOW(), '%M %Y');
SELECT DATE_FORMAT(a.date, '%M/%Y') FROM t1 AS a;
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
sqlfiddle: http://sqlfiddle.com/#!2/6a68f/2

Varchar date in SQL query

I have a varchar column called date_submitted and it's formatted like so:
03-06-2014 4:32 pm
02-14-2014 2:44 am
And so on...
I was wondering how I would get all of the dates from a certain date or from something like 20 days ago. This select statement is something along the lines of what I need.
SELECT date_submitted FROM myTable WHERE date_submitted < 02-14-2014 12:00 am
try this with DATE_SUB
SELECT date_submitted
FROM myTable
WHERE STR_TO_DATE(date_submitted, '%m-%d-%Y') > DATE_SUB(DATE_FORMAT((NOW(),'%m-%d-%Y'), INTERVAL 20 DAY)
You need to CONVERT your datetime column appropriately like this:
SELECT date_submitted FROM myTable
WHERE convert(datetime,date_submitted,100) < DATE_SUB(convert(datetime,'02-14-2014 12:00 am',100), INTERVAL 20 DAY)
You need something like this, or else you'll be comparing a string to a date, you won;t get good results
SELECT date_submitted
FROM myTable
WHERE
date_format(str_to_date(date_submitted,'%m-%d-%Y %h:%i %p'), '%Y-%m-%d %H:%i:%s')
>DATE_SUB(NOW(), INTERVAL 20 DAY);

DATE_SUB return date in specif format

Part of my SQL query
DATE_SUB(dStartDate,INTERVAL 30 DAY) AS dEarlyBirdExipiryDate
It return dEarlyBirdExipiryDate as 2013-05-16
But I want it in 16 May, 2013 format.
What changes I should make in my query to get this?
Use DATE_FORMAT() function:
SELECT DATE_FORMAT( DATE_SUB(dStartDate, INTERVAL 30 DAY)
, '%d %M, %Y'
) AS dEarlyBirdExipiryDate;
See this SQLFiddle
You can use something like this. Em not sure but it would work with a few tweaks.
SELECT DATE_FORMAT(DATE_SUB(DATE('2007-11-30 09:00:00'), INTERVAL 1 DAY), '%d %M, %Y'); //