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.
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
I have a simple SQL query which will search for matched rows between two date range. Date format to be searched for is 'yyyymm' -> year-month.
I have written the following query but it is giving me error
SELECT *
FROM `my_table`
WHERE added_on BETWEEN EXTRACT( YEAR_MONTH FROM `added_on` )='201606' AND
EXTRACT ( YEAR_MONTH FROM `added_on` )='201706'
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '='201606' AND EXTRACT ( YEAR_MONTH FROM `added_on`
)='201706' LIMIT 0, 30' at line 1
I don't think there is a need to use EXTRACT at all in this case. Instead you can try:
SELECT *
FROM `my_table`
WHERE added_on BETWEEN '20160601' AND '20170630'
Try this query
SELECT *
FROM `my_table`
WHERE EXTRACT( YEAR_MONTH FROM `added_on` ) BETWEEN EXTRACT( YEAR_MONTH FROM '201606' ) AND EXTRACT( YEAR_MONTH FROM '201706' )
'201606' and '201706' should be user input.
If added_on column datatype is date, write EXTRACT( YEAR_MONTH FROM added_on )
If not date datatype, just write column name
Try DATE_FORMAT(date,'%Y%m') /EXTRACT(YEAR_MONTH FROM date) to achieve yyyymm
Using DATE_FORMAT
SELECT *
FROM `my_table`
WHERE DATE_FORMAT(added_on,'%Y%m') BETWEEN '201606' AND '201706'
Using EXTRACT
SELECT *
FROM `my_table`
WHERE EXTRACT(YEAR_MONTH FROM added_on) BETWEEN '201606' AND '201706'
Without using them
Add 01 to the first date and 31 to the second date.This represent the first and last days of the month
SELECT *
FROM `my_table`
WHERE added_on BETWEEN '20160601' AND '20170631'
You can use below query -
SELECT * FROM `my_table` WHERE added_on BETWEEN REPLACE(SUBSTRING_INDEX(added_on,'-',2),'-','')='201606' AND REPLACE(SUBSTRING_INDEX(added_on,'-',2),'-','')='201706';
I get the error:
#1111 - Invalid use of group function.
I can't seem to understand what's wrong. My code is below:
Select * from (Select MAX(ticket_comment_date)
from ticket_movement_tran` group by ticket_id) AS T
where DATE(MAX(ticket_comment_date))
between DATE_FORMAT( STR_TO_DATE( '01-01-2014', '%d-%m-%Y' ) , '%y-%m-%d' )
and DATE_FORMAT( STR_TO_DATE( '01-11-2014', '%d-%m-%Y' ) , '%y-%m-%d' );
issue
ticket_id ( which is being grouped by in the inner query ) is not included in the selected fields. You can't group by a field which is not selected, hence the error message..
fix
add ticket_id to fields selected
no need for two layers of sql, can do this in one layer; adding the date filter logic in the having clause ( as it relates to the aggregated value )
adjusted query
select ticket_id, MAX(ticket_comment_date)
from ticket_movement_tran
group by ticket_id
having DATE(MAX(ticket_comment_date)) between
DATE_FORMAT( STR_TO_DATE( '01-01-2014', '%d-%m-%Y' ) , '%y-%m-%d' )
and DATE_FORMAT( STR_TO_DATE( '01-11-2014', '%d-%m-%Y' ) , '%y-%m-%d')
;
notes
I assume since you are using DATE(MAX(ticket_comment_date)) that ticket_comment_date is not properly defined as a date. Would recommend changing the storage type for this field to a DATE..
So if the ticket_comment_date is currently a character field, then MAX(ticket_comment_date) can give you a different results than you expect. in particular lexical ordering ( ordering of character fields ) can differ from numerical or date based ordering
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
How do I select data between a date range in MySQL. My datetime column is in 24-hour zulu time format.
select * from hockey_stats
where game_date between '11/3/2012 00:00:00' and '11/5/2012 23:59:00'
order by game_date desc;
Returns nothing despite having data between these time periods. Do I have to force the values in the 'from' and 'to' fields to datetime type in the query?
You need to update the date format:
select * from hockey_stats
where game_date between '2012-03-11 00:00:00' and '2012-05-11 23:59:00'
order by game_date desc;
Here is a simple way using the date function:
select *
from hockey_stats
where date(game_date) between date('2012-11-03') and date('2012-11-05')
order by game_date desc
A simple way :
select * from hockey_stats
where game_date >= '2012-03-11' and game_date <= '2012-05-11'
You probably need to use STR_TO_DATE function:
select * from hockey_stats
where
game_date between STR_TO_DATE('11/3/2012 00:00:00', '%c/%e/%Y %H:%i:%s')
and STR_TO_DATE('11/5/2012 23:59:00', '%c/%e/%Y %H:%i:%s')
order by game_date desc;
(if game_date is a string, you might need to use STR_TO_DATE on it)
In a simple way it can be queried as
select * from hockey_stats
where game_date between '2018-01-01' and '2018-01-31';
This works if time is not a concern.
Considering time also follow in the following way:
select * from hockey_stats where (game_date between '2018-02-05 01:20:00' and '2018-02-05 03:50:00');
Note this is for MySQL server.
MySQL date format is this : Y-M-D. You are using Y/M/D. That's is wrong.
modify your query.
If you insert the date like Y/M/D, It will be insert null value in the database.
If you are using PHP and date you are getting from the form is like this Y/M/D, you can replace this with using the statement .
out_date=date('Y-m-d', strtotime(str_replace('/', '-', $data["input_date"])))
You can either user STR_TO_DATE function and pass your own date parameters based on the format you have posted :
select * from hockey_stats where game_date
between STR_TO_DATE('11/3/2012 00:00:00', '%c/%e/%Y %H:%i:%s')
and STR_TO_DATE('11/5/2012 23:59:00', '%c/%e/%Y %H:%i:%s')
order by game_date desc;
Or just use the format which MySQL handles dates YYYY:MM:DD HH:mm:SS and have the query as
select * from hockey_stats where game_date between '2012-03-11 00:00:00' and'2012-05-11 23:59:00' order by game_date desc;
You must search date defend on how you insert that game_date data on your database.. for example if you inserted date value on long date or short.
SELECT * FROM hockey_stats WHERE game_date >= "6/11/2018" AND game_date <= "6/17/2018"
You can also use BETWEEN:
SELECT * FROM hockey_stats WHERE game_date BETWEEN "6/11/2018" AND "6/17/2018"
simple as that.