I have a MySQL table, one of rows is filled using current unixtime (let's call it 'unixtime').
I need to select values, but using datetime format in the query.
For example:
SELECT * from test where unixtime>"2017-01-06 12:00" AND unixtime<"2017-01-08 12:14"
Note: Datetime range could be different.
Thank you very much!
As mentioned above, by using FROM_UNIXTIME() in your query. The following query selects from a table called 'testValues' and a Unix time field in the table, which is called 'timevalue' (stored as an int). It selects between two date ranges with the ranges in DateTime format in the query.
SELECT * FROM testValues
where FROM_UNIXTIME(timevalue) > "2017-01-07 21:00:48"
AND FROM_UNIXTIME(timevalue) < "2017-01-07 21:02:09"
Hope that helps :)
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 have a the following query :
select * from table where createddate>='03-Feb-2020' and createddate<'04-Feb-2020'
The above query will give me incremental count for a single day.
How do i generalize the above query so that i can get the entire historical data/full dump without changing the where clause.
For example:
select * from table where createddate>='VARIABLE1' and createddate<'VARIABLE2'
Is there a way that without changing the schema of the sql query i can just pass in different values for the createddate to get the full dump?
Is this what you want?
where createddate >= '1000-01-01' and createddate < '9999-12-31'
Note that dates in MySQL must be formated as YYYY-MM-DD.
Do you want group by?
select date(createddate), count(*)
from table
group by date(createddate);
This returns the count for each day.
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 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";