I hava a table with t.string :created_time column
It contains data in UTC format (e.g. 2017-05-10T06:30:01+0000)
I have to retrieve records from table with certain date but time doesn't matter
date = Date.new(2017,05,10)
nodes = Node.where(created_time: date)
logger.info nodes.count
Here I have 0 count. How to make additional processing of created_time value so I could to compare only date part?
Create a date range and query like this
date = Date.new(2017,05,10)
nodes = Node.where(created_time: date.beginning_of_day..date.end_of_day)
You will get the records for that day ;)
UPDATE
Why on earth is created_at a string??
Let's try this,
Node.where('DATE(nodes.created_time) = ?', date)
Select the date as just date from mysql table like this
SELECT DATE(date_time_field) FROM `yourtable`;
Related
I am trying to analyze order_Date column and column have multiple date format i want to convert all those date in same format which wull make be easier to analyze the order_date.
I am trying to analyze the order_date however this column have multiple date format 2019/07/15 and 1/13/2014
Howeever, while converting different format date with one format yyyy/mm/dd with query.
select date_format(order_date, '%y/%m/%d'),orderid from superstore;
it shows null values like this.
i have tried to use `CAST as well but it shows every single value as null.
select case when order_date like '%Y' then date_format(order_date, '%Y/%m/%d') else null end as newdate from superstore;
date_format funtion is used to format a date datatype you should use https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date any null values returned by str_to_date either failed or started as null. You will need to examine these and adjust the str_to_date parameters appropriately. There is a catch though is 20/2/20 y/m/d or d/m/y (for example) and how can you differentiate month and day where both are <=12?
For example
drop table if exists t;
create table t
(dt varchar(10));
insert into t values
('1/1/2020'),('2020/1/12'),('12/12/12'),(null),('13-14-15');
select dt,
case when length(substring_index(dt,'/',-1)) = 4 then str_to_date(dt,'%d/%m/%Y')
when length(substring_index(dt,'/',1)) = 4 then str_to_date(dt,'%Y/%m/%d')
when length(substring_index(dt,'/',1)) = 2 then str_to_date(dt,'%y/%m/%d')
else str_to_date(dt,'%y/%m/%d')
end dateformatted
from t;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=553219f33ad9e9a4404fc4c0cb6571c9
note in no case can I identify month and day and sometimes year..
I have stored this kind of format 2022-02-06 18:40:00 in my trans_reminder_date. I want to use only date in where condition but with this condition i am not able to fetch data
$today = date('Y-m-d');
SELECT * FROM sales_detail
WHERE trans_reminder_date = '".$today."'
AND trans_reminder_date != ''
ORDER BY sales_detail_id DESC";
If when your filter parameter is a string:
select * from sales_detail
where cast(trans_reminder_date as date) = cast('2020-03-22' as date)
if you want to use the current date for filtering then MySQL has a function that getting only the current date without time.
select * from sales_detail
where cast(trans_reminder_date as date) = curdate()
On MySQL for converting other types to another, you can use a cast
P.S.
Starting with MySQL 8.0.13 we have now an easiest way to create functional indexes. When you are using cast(updated_at as date) then DB will not use index for column updated_at. You must create a functional index for best performance.
The following query will give you all the result for the current date. By doing this there won't be any need to cast values.
SELECT
*
FROM
sales_detail
WHERE
trans_reminder_date >= curdate()
AND trans_reminder_date < curdate() + INTERVAL '1' DAY
ORDER BY
sales_detail_id DESC;
Using Cast function to change datetime type to date example
Cast(column_name as date)
Or
You using convert function change datetime type to date
CONVERT(column_name, date);
CONVERT(expression, datatype);
OR,
CONVERT(expression USING character_set);
character_set: It specifies the desired character set in which we want to be converted.
Data type : It specifies the desired data type in which we want to be converted.
Expression : It is a specified value going to be converted into another specific datatype.
If I consider the following:
Customer | Date
100 | 09-06-2021
100 | 17-03-2020
I am trying to find out the most recent date from the above, by using: select MAX(Date) from table, and it is returning 17-03-2020, which is wrong.
How do I get the most recent date as 09-06-2021?
What is wrong is that you are storing the date as a string, not a date.
You can fix your immediate problem by doing:
select max(str_to_date(date, '%d-%m-%Y'))
from t;
But you should really fix the data. Start by putting the date in a canonical format:
update t
set date = str_to_date(date, '%d-%m-%Y');
Then modify the column to the correct data type:
alter table t modify column date date;
I have the following mysql table:
tasks:
=====================
tid
status
desc
duedate
And i have the following records in that table:
records
===========================
1
active
Test description
08/15/2014
2
active
Another description
08/31/204
I am trying to get the days that there is a task for, in that particular month. I have the following query but when i run it it gets both records but "day" is null on both of them for some reason. Can someone please help me with this.
MYSQL QUERY
====================
SELECT DATE_FORMAT(due_date,'%d') AS day FROM tasks WHERE due_date BETWEEN '08/01/2014' AND '08/31/2014'
Try:
SELECT DAY(due_date) AS day
FROM tasks
WHERE due_date >= '2014-08'
AND due_date < '2014-09';
DAY() is a better function for what you want and I prefer using >= and < than BETWEEN for date comparisons, as it allows you to specify precise ranges more easily. Here, for example, you don't need to know the number of days in the month.
I have also used the default date format, which is preferable. If you need the, in my opinion, cray American date format, use DATE_FORMAT() in your SELECT.
This will only work with DATE, DATETIME and TIMESTAMP columns, which is how your due_date should be stored, preferably DATE.
UPDATE
To convert the VARCHAR column to DATE run:
UPDATE tasks SET due_date=STR_TO_DATE(due_date,'%m/%d/%Y')
Then change the type. Also remember to change your INSERT statements to use the default format.
You've got to convert those "date" strings to proper date values with STR_TO_DATE:
SELECT
DAY(STR_TO_DATE(due_date,'%m/%d/%Y')) AS day
FROM tasks
WHERE
STR_TO_DATE(due_date, '%m/%d/%Y')
BETWEEN STR_TO_DATE('08/01/2014' '%m/%d/%Y')
AND STR_TO_DATE('08/31/2014', '%m/%d/%Y')
else you're comparing strings instead.
Note:
It would be better to use a proper DATE or DATETIME column instead.
With the current VARCHAR format MySQL is unable to use indexes. That's very bad for performance.
You can convert your data by adding another column to your table:
ALTER TABLE tasks
ADD COLUMN new_due_date DATE;
Then you use an UPDATE statement to fill this new column
UPDATE tasks
SET new_due_date = STR_TO_DATE(due_date, '%m/%d/%Y');
If you don't need your old column anymore then you can delete this column and modify the new column to have the name of the old one. Then you will have your table with all your data in a DATE column.
I have a table with columns as
Id (1)
Date holding values as (2013-12-12 00:00:00)
Start time (00:00:00)
end time (01:00:00)
value
The user will give the date range and will specify day like sunday ,monday etc .
How can I use a sql query to filter the dates and find the proper days between that for the specified days as well.
One way would be to use BETWEEN ... AND ... and DAYOFWEEK():
SELECT *
FROM my_table
WHERE Date BETWEEN ? AND ?
AND DAYOFWEEK(Date) = ? -- 1=Sunday ... 7=Saturday