I am facing a silly problem while converting datetime column into y-m-d format.
I am running following query :
SELECT STR_TO_DATE(dateadded, '%y-%m-%d') FROM my_table
Note : The dateadded column is a type of datetime.
Whenever I am running above query it always shows (NULL).
Can somebody solve this issue as this is irritating me ?
Since your column is of datetime type rather than string type you should use DATE_FORMAT function as below:
SELECT DATE_FORMAT(dateadded, '%y-%m-%d')
FROM my_table
This query will work for a four digit year in table
SELECT STR_TO_DATE(dateadded, '%Y-%m-%d')
FROM my_table
while this will work for a two digit year
SELECT STR_TO_DATE(dateadded, '%y-%m-%d')
FROM my_table
you can try this by following query
SELECT
STR_TO_DATE('99-12-12', '%y-%m-%dd'),
STR_TO_DATE('1999-12-12', '%Y-%m-%dd')
both output will be
1999-12-12
for further details
Related
I wish to sort my table with date order so that recently added data will be on the top of the table.
I have used query for sorting as:
select date from register_table order by date desc.
Currently table display data as:
date
02.04.2019
05.04.2019
09.04.2019
10.04.2019
06.02.2019
23.01.2019
11.01.2019
I expect my table to display as:
date
10.04.2019
09.04.2019
05.04.2019
02.04.2019
06.02.2019
23.01.2019
11.01.2019
How to display data in date order?
Your fundamental problem is not storing the date as a date. You should fix that.
For the query to work, use:
order by str_to_date(date, '%m.%d.%Y')
To fix the data, you can do:
update register_table
set date = str_to_date(date, '%m.%d.%Y');
alter table register_table
modify date date;
You can see how this works here.
I don't know why your date is stored like that but here, give this a try:
SELECT date FROM date ORDER BY STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') DESC;
If you want to see what exactly happen, run this query:
SELECT date,STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') FROM date;
In case you still don't quite understand, refer to MySQL STR_TO_DATE function and MySQL REPLACE function.
I have a column where the dates are type varchar. For example:
15-10-2018
16-10-2018
19-10-2018
23-10-2018
29-10-2018
8-11-2018
9-11-2018
10-11-2018
12-11-2018
when I consult with the following query
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '9-11-2018'.
I have the right result.
15-10-2018
16-10-2018
19-10-2018
23-10-2018
29-10-2018
8-11-2018
9-11-2018
but if the query is:
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '10-11-2018'.
or
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '12-11-2018'.
The answer I get is empty.
I think it's only validating the days in the sql.
I need to get the right dates.
I think the problem is the fact that the column is varchar, so it's comparing characters instead of a range of dates. I will recommend convert the column to date type and try again.
Alternative if you cannot change the type of the column you could cast it to date format like this:
SELECT DISTINCT `date` FROM `test` WHERE STR_TO_DATE(`date`,'%d-%m-%Y') BETWEEN '2018-10-15' AND '2018-11-10';
I tested with your data and it works. Of course this could put some extra effort on the database and will not use indexes.
You need to set the datatype to date and update your dates to be using date for a more reliable result. Once done you should be using the database format for the dates in your WHERE clause.
Try
SELECT DISTINCT date FROMtestWHERE date BETWEEN '2018-10-15' and '2018-11-10'
I have a mysql column where the data is stored as VARCHAR though the data values are of datetime in the format of yyyy-mm-dd hh:mm:ss.
Now my task is to group by the date part i.e yyyy-mm-dd by converting VARCHAR to date-time and then just taking date part out of it
QUERY
SELECT SUM(value)
FROM table
GROUP BY name , [date part of the varchar field]
Please let me know if this is at all possible and if yes, how?
Assuming that your data in this varchar field is properly formatted, you can work with the left function, like this:
SELECT LEFT(mydate, 10) AS myval,
SUM(myvalue)
FROM mytable
GROUP BY myval;
If this isn't a big issue; I'd advise converting your varchar column to datetime or timestamp. If not only for the possibly better data storage usage, it'll be way easier to do work with date and time related functions.
Just use the left function. You can leave the date as a string:
SELECT left(datecol, 10) as YYYYMMDD, SUM(value)
FROM table
GROUP BY left(datecol, 10);
I removed name from the group by because it doesn't seem relevant to the question. You can, of course, add it back in.
By the way, MySQL understands this format for dates, so if you really, really want a date:
SELECT date(left(datecol, 10)) as RealDate, SUM(value)
FROM table
GROUP BY RealDate;
I am trying folowing on my_table where modifiedtime is of type datetime
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y') between '05-11-2013' and '28-11-2013';
The query gives me some other record too which are not falls between above dates, for example there is a record in result set dated '04-01-2014'
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y')='05-11-2013'
this query works fine and gives all the records for the given date
why the first behaves like that?
How can i correct it?
what is the efficient way to implement it?
such that i can get all the records only between given two dates.
SELECT
DATE_FORMAT(modifiedtime, '%d-%m-%Y')
FROM
my_table
WHERE
modifiedtime BETWEEN STR_TO_DATE('05-11-2013', '%d-%m-%Y') AND STR_TO_DATE('28-11-2013', '%d-%m-%Y');
DATE_FORMAT() returns TEXT type column and dates can't be applied.
Use without DATE_FORMAT in the WHERE
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where modifiedtime between '05-11-2013' and '28-11-2013';
you DATE_FORMAT function converts the column modifiedtime to String.
and hence in your first query you do a string comparison rather then a date comparison.
Also your date literal is not incorrect. It must be of form YYYY-MM-DD
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where cast(modifiedtime as date) between '2013-11-05' and '2013-11-28';
I'm trying to make some vchar values searchable based on a date.
The strings I have to work with look like this:
1/7/2006 12:45:24 AM
1/7/2006 1:18:36 AM
1/7/2006 1:21:43 AM
1/7/2006 1:32:09 AM
3/30/2006 12:32:10 PM
3/30/2006 1:19:30 PM
3/30/2006 1:20:44 PM
So first off let's get rid of the AM.. PHPMyAdmin the sql query is:
SELECT trim('AM' FROM `orderdate`) FROM tblorders
This works to get rid of the AM now let's set the values as a variable and try to wrap a string to str_to_date() around the results:
SELECT trim('AM' FROM `orderdate`) AS `value`, STR_TO_DATE(`value`,'%d,%m,%Y') FROM tblorders
This yields value as an unknown column. How else do you string two function values together so as to then use them to be filtered such as WHERE value > 2/1/2006 ?
You can compose functions like this:
select str_to_date(trim('AM' from orderdate), '%m/%d/%Y')
Note that I also corrected your date format to match your data. You don't actually need to trim those values to use str_to_date on them, just this will work fine:
select str_to_date(orderdate, '%m/%d/%Y')
If you want to use that in your WHERE clause then put the function calls in there:
select trim('AM' from orderdate), str_to_date(orderdate, '%m/%d/%Y')
from your_table
where str_to_date(orderdate, '%m/%d/%Y') > '2006-01-02'
and let the optimizer recognize the repetition or use a derived table:
select value, the_date
from (
select trim('AM' from orderdate) as value, str_to_date(orderdate, '%m/%d/%Y') as the_date
from your_table
) dt
where the_date > '2006-01-02'
Note the use of ISO 8601 dates in the query, that format is unambiguous and any database worth using will understand it regardless of your locale settings.
I'd also recommend that you fix your schema to use real timestamps instead of those strings.