i am trying to run a query in mysql to select all records from a table where the "end_date" of that record is greater than or equal to the current date, but it keeps coming up with "MySQL returned an empty result set". But such a record definitely exists n my table, is there a problem with my query?
this is the value in the end_date column for the one record "03/24/2014".
and this is my query
SELECT * FROM ******** WHERE DATE(end_date) > DATE(CURDATE())
I have my suspicions however that it may be the date format meaning the date format on my computer might be like this: dd/MM/yyyy
Whereas the date format on the server might be like this : MM/dd/yyyy
Unfortunately if that is the case i dunno what to do about that yet.
As noted in comment by #FreshPrinceOfSO, better to store date as real DATE, TIMESTAMP, or DATETIME.
If that's not an option--it's not your db to alter for example--then Mysql has a STR_TO_DATE() function you can use:
SELECT * FROM ********
WHERE STR_TO_DATE(end_date,'%m/%d/%Y') > CURDATE();
HOWEVER...
If end_date column has an index, the function applied to it will nullify the index benefit.
Related
I have a table with 2 timestamps: start_time and end_time. How can I query with conditions like select all where the diff of those 2 fields is more than X hours.
Also does the field type (timestamp vs datetime) has any impact on the query i'm trying to achieve?
The data type of the fields does for sure have some differences.
As stated in MySQL Timestamp Difference, it is usually the case that, given two datetime fields, they get converted to timestamps in order to subtract them.
The query could be something like
SELECT *
FROM Table
WHERE TIMESTAMPDIFF(HOUR,start_time,end_time)>2
Edit:
What written above is valid for MySQL, you can use DATEDIFF if you are working in SQL Server
Is there any difference in any sql engines (and particularly in mysql) in the following two queries?
SELECT * FROM table where date = '2019-01-01'
And:
SELECT * FROM table where date = DATE('2019-01-01')
Doing an explain returns the same result, but perhaps there's some sort of difference that I'm not catching? I need to run a query against a multi-billion row table and am trying to optimize it before running.
There should not be. The expression DATE('2019-01-01') should be evaluated during the compilation phase turning the result into a date. Similarly, the constant value '2019-01-01' is implicitly converted to a date for the comparison.
This allows MySQL (and most other databases) to use indexes and partitions defined on that column.
Date() function Extracts the date part of a date or date/time expression
for example the value of the field name BirthTime is "2017-09-26 16:44:15.581"
so you have to use the following query to check the date :
SELECT DATE(BirthTime)
result is : 2017-09-26
I have a MYSQL database where data is stored for particular users.The database also records the date and time when the user has inserted the data. I want to query and check if the user has not inserted any data for the previous dates. i.e. I want to know if the user has not inserted any data on a particular date and get that date for which no data has been inserted.Is it possible to get the date in that manner? I am trying the below query but it doesn't work.
SELECT date from employee_details where date < NOW() && date IS NULL;
If no data exists in employee_details for the give date (i.e. the user has done nothing on the given date) your query will not return anything.
You may create a separate table containing nothing but dates and use this to do an outer join to determine if no user data exists for the give day.
You are literally looking for a date that is before this date AND the date has not been filled in, so that code won't work. You probable want to replace the date IS NULL to empty_data_column IS NULL if that is what you want
Solution:
select * FROM employee_details WHERE date <= CURDATE() OR date is null;
This means:
select *
(select everything)
from employee_details
(from table_name)
where date <= CURDATE() OR date is null;
(date is less or the same as the actual date OR the date is null)
Note:
in case you want both conditions at the same time, substitute or with and.
CURDATE() returns the DATE part of the current time. Manual on CURDATE()
NOW() returns the date and time portions as a timestamp in various formats, depending on how it was requested. Manual on
NOW().
I am stuck in a situation where I am reading data from CSV file through 'LOAD DATA LOCAL INFILE' and storing it in Mysql table.
The date column in my table is of type string.
The below query is not working if my Date format is 'yy/dd/MM', it only returns 2 records
select column1, column2 from myTable where date between '16/08/15' and '16/08/20';
and if I ran this:
select column1, column2 from myTable where date > '16/08/15';
It return all records.
Is there a way to ran the first query so I can specify start and end date ?
Turn your dates into 'yyyy-mm-dd' format before using them inside queries, and it will be easier.
Instead of passing the year as yy try to pass it as yyyy in the date string since my sql stores dates in that format.Since every year is a four digit one, giving a two digit year in the where clause in order to pull out all records greater than that year will actually pull out all the records.
Since you say your date column is of type "String" and not DATE so in your query mysql is not operating the between function on dates, instead it's operating it on strings. You probably need to cast you string dates to actual dates before applying the between function. see e.g. str_to_date function.
I am having my date field in Mysql which is stored as char is as follows 050712.. Now I would like to display the results which are available in the database which are less than this date. I write as follows
Condition should fail
select * from tblFedACHRDFI where date_format(changedate,'%m/%d/%Y')> 05/08/12;
This is displaying all records which are available but I don't need that I would like to display only when date is 05/06/12 which means
True Condition
select * from tblFedACHRDFI where date_format(changedate,'%m/%d/%Y')> 05/06/12;
The same worked for me in Sqlserver when I write as follows
Records not getting displayed which is true as per my requirement
select * from tblFedACHRDFI where
CONVERT(datetime,(SUBSTRING(ChangeDate,1,2)+'/'
+SUBSTRING(ChangeDate,3,2)+'/'+dbo.Years
(SUBSTRING(ChangeDate,5,2))+SUBSTRING(ChangeDate,5,2)))>
'05/08/2012'
So can any one help me where I went wrong in MySql statement..
A MySQL date should be YYYY-MM-DD, column type should be DATE.
If you wish to store a date any other way (for example, a CHAR(6) as you do here), you'll have to use conversions each time you use the date. This is slower, uses more CPU, and can fail because you can store invalid values in your CHAR field.
It does work, however. Use the STR_TO_DATE function to convert your CHAR column to a proper date. Now you can compare it against a proper date, use INTERVAL functions, the whole shebang:
select *
from tblFedACHRDFI
where str_to_date(changedate,'%m%d%Y') > "2012-08-05";