My query is this. I have a bunch of entries and i want to group them by date. But instead of having date in my database, I have a datetime field. What do I do?
select * from follow_queue group by follow_date cast follow_date as date
That's not working.
Use DATE() function:
select * from follow_queue group by DATE(follow_date)
Related
I've view named Sales View
select * from SalesView;
I want to fetch records from SalesView having order_date 9-OCT-2019.
Value for Order date is supplied from GUI in format DD-MM-YYYY.
I tried,
select *
from salesView
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
order by oID;
I've also tried date_format() instead of str_to_date(), but It is returning nothing.
Check your where clause
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
Replace order_date's format with the format in which database stored it. YYYY-MM-DD
where str_to_date(order_date,'%Y-%m-%d') = str_to_date('09-10-2019','%d-%m-%Y')
I want to sort a date-of-birth column in a table which has date stored in format of %m/%d/%Y . When I use order by , the column values are being treated and sorted as string and not date. I have tried UNIX_TIMESTAMP() function but it also seems to be not working. What are the possible solutions ?
Use STR_TO_DATE() Function
SELECT * FROM yourtable
ORDER BY STR_TO_DATE(DueDate,'%m/%d/%Y ') DESC
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 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
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';