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.
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
I want to fetch data from mysql database weeknumber in where clause
like
SELECT * FROM table WHERE addedon(WEEK(2))
I have week numbers of year and I need to fetch data by that week number.
SELECT * FROM table WHERE WEEK(addedon) = ? where ? is number of week which you want filter by
Also WEEK() receives second parameter mode and can return different values. Documentation
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.
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";
I have a MySQL database with two fields: Date and Value.
My dates are quarters (Q1-2007, Q2-2008...). I want to be able to use the following SQL query:
SELECT * FROM table WHERE 1 ORDER BY Date
How can I do this?
I'm guessing you want them ordered so that all the 2007 ones appear in order followed by the 2008 ones, etc. This should work:
SELECT * FROM table WHERE 1 ORDER BY SUBSTRING(Date, 4, 4), SUBSTRING(Date, 1, 2);
Assuming your date formats are all consistent and that your 'Date' column is actually a VARCHAR or similar...
Use a date-column and decide on a date for each quarter. Then you can use the builtin functions for dates.
Doesn't SELECT * FROM table WHERE 1 ORDER BY Date already sort the field Date as Q1-2007, Q2-2007, Q3-2007, Q4-2007? If you sort such strings, that should be how they are sorted.
If then I would create a database to contain those values, I would rather use 3 fields, and use two different fields for quarter, and year.