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
Related
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
What I have:
Scenario 1:
SELECT `agent_id`, COUNT(`id`) AS total_users
FROM `users`
WHERE FROM_UNIXTIME(`create_at`, '%m-%d-%Y') <= '01-01-2016'
GROUP BY `agent_id`
Result:
agent_id | total_users
------------------------
foo | 0
bar | 0
Scenario 2:
SELECT `agent_id`, COUNT(`id`) AS total_users
FROM `users`
WHERE FROM_UNIXTIME(`create_at`, '%m-%d-%Y') <= '12-31-2015'
GROUP BY `agent_id`
Result:
agent_id | total_users
------------------------
foo | 532
bar | 435
The problem
The total_users column for the date 01-01-2016 is always 0. But when I change the value from 01-01-2016 to 12-31-2015, the value is different, it returns many users. It looks like that the data messes up when the year changes.
In you example:
FROM_UNIXTIME(`create_at`, '%m-%d-%Y') <= '01-01-2016'
It is not compared as DATE, but as string. That is why 01... gives 0 while 12... gives many rows. Because 12 is bigger than 01 as a string.
Therefore, to make it right, you can covert both to a DATE format:
SELECT `agent_id`, COUNT(`id`) AS total_users
FROM `users`
WHERE FROM_UNIXTIME(`create_at`) <= STR_TO_DATE('01-01-2016','%m-%d-%Y');
GROUP BY `agent_id`
This is solution, convert your comparision date to unix time stamp and check with create_at
SELECT `agent_id`, COUNT(`id`) AS total_users
FROM `users`
WHERE `create_at` <= UNIX_TIMESTAMP('01-01-2016')
GROUP BY `agent_id`
and
SELECT `agent_id`, COUNT(`id`) AS total_users
FROM `users`
WHERE FROM_UNIXTIME(`create_at`, '%m-%d-%Y') <= STR_TO_DATE('01-01-2016','%m-%d-%Y')
GROUP BY `agent_id`;
in second query there is two function call FROM_UNIXTIME() and STR_TO_DATE() but in first query there is only one function call UNIX_TIMESTAMP(). which solution is suitable you can use
Hope this will work for you
Unless you use STR_TO_DATE to a date string while comparing with another date it is considered to be string comparison.
Try the following query please :
SELECT `agent_id`, COUNT(`id`) AS total_users
FROM `users`
WHERE FROM_UNIXTIME(`create_at`, '%m-%d-%Y') <= STR_TO_DATE('01-01-2016','%m-%d-%Y')
GROUP BY `agent_id`;
Check this too:
SELECT '01-01-2016' > '12-31-2015';
Result: 0 i.e. FALSE. [ Because it's string comparison not date comparison.]
Now look at this:
SELECT STR_TO_DATE('01-01-2016','%m-%d-%Y') > STR_TO_DATE('12-31-2015','%m-%d-%Y')
Result: 1 i.e. TRUE.[ Because it's a date comparison now.]
The answers above didn't work.
I don't know why but, this solution instead seemed to work:
instead of using
WHERE FROM_UNIXTIME(create_at, '%m-%d-%Y') <= '01-01-2016'
I just omitted the second parameter of the function and reformatted the date to Y-m-d
So my where clause now looks like this
WHERE FROM_UNIXTIME(create_at) <= '2016-01-01'
Weirdly it worked. Anyways, thanks for those who tried to help me.
I have this query:
SELECT *
FROM some_table
WHERE id != 1 AND
(event_date BETWEEN '20/06/2015' AND '01/07/2015')
ORDER BY the_date
The result is 0.
If I try this query:
SELECT *
FROM events
WHERE id != 1 AND
(event_date BETWEEN '20/06/2015' AND '29/06/2015')
ORDER BY the_date
It works.
What is the problem with the difference between tow month (Juny and July).
Thanks.
Seems like you're comparing these dates lexicographically. Assuming that event_date is a date column, use str_to_date to convert the string literals to dates:
SELECT *
FROM some_table
WHERE id != 1 AND
(event_date BETWEEN STR_TO_DATE('20/06/2015', '%d/%m/%Y') AND
STR_TO_DATE('01/07/2015', '%d/%m/%Y'))
ORDER BY the_date
Write date constants in the ISO standard YYYY-MM-DD format:
SELECT *
FROM some_table
WHERE id != 1 AND
event_date BETWEEN '2015-06-20' AND '2015-07-01'
ORDER BY the_date
MySQL uses the following data types for saving a date:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
Try changing the date format
i tried to use this sql:
SELECT date_format(signdate , '%Y-%c-%d %h:%i:%s' ) as post_date ,
date_format(NOW(), '%Y-%c-%d %h:%i:%s' ) as post_date_gmt
FROM `article` where outkey = 'Y'
the result of post_date is null, but post_date is working. so how can i use the funciton date_format to format signdate (ex: signdate is 1095959971)?
DATE_FORMAT expects a DATETIME column.
You'd have to cast it:
DATE_FORMAT(CAST(signdate AS DATETIME), ....)
It would be better to have a real DATETIME field, though.
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 '%,%'