I want to make a query which returns me some values between two dates and two times...
The DATE and TIME values are in different columns, DATE is called FECHA and TIME is called HORAIN
I have 4 possible values with just a DATE query:
Value 1 has Date 2012-05-07 time 02:45:00
Value 2 has Date 2012-05-07 time 12:00:00
Value 3 has Date 2012-05-08 time 04:35:00
Value 4 has Date 2012-05-08 time 07:30:00
What I want to get, is the results from 07:00:00 to 23:59:59 from the first day, and values from 00:00:00 to 06:59:59 from the second day (so an interval from 07 AM to 06:59 AM)
This is my query, which is returning NO VALUES... It must return me VALUE 2 and VALUE 3, because it matches my desired requirements
SELECT * FROM mytable
WHERE
(DATE(fecha)>='2012-05-07' AND TIME(horain)>='07:00:00')
AND
(DATE(fecha)<='2012-05-08' AND TIME(horain)<='06:59:59');
What am I doing wrong? :(
You can fetch result using Concat(). It concatenating two fields value.so You can concatenate fecha with horain field value and then search for results like given below .
SELECT * FROM `mytable` WHERE (Concat(`fecha`, ' ', `horain`, '') <= '2012-05-08 06:59:59') AND (Concat(`fecha`, ' ', `horain`, '') >= '2012-05-07 07:00:00')
Concat() function take 4 parameter.
Value1
String from which concatenate value1 to value2
Value2
String want to concatenate in last
Hopefully it will be helpful for you
thanks.
Try to debug it one step at a time - eliminate as much restrictions and see if you get records - when you get them, add one condition at a time:
start with:
SELECT * FROM mytable
continue with:
SELECT * FROM mytable
WHERE
DATE(fecha)>='2012-05-07'
and so on, until you find the condition that cuts out all your records and see why that is happening
Try SELECT * FROM mytable WHERE FECHA >= 2012-05-07 AND horain >= 07:00:00)
AND FECHA <= 2012-05-08 AND horain <= 06:59:59 );
The time and date are checked separately
If we restructure your where clause and leave out the date part you get:
SELECT * FROM mytable
WHERE TIME(horain)>='07:00:00' AND
TIME(horain)<='06:59:59'
This will never give you a result since both can never be true.
what you can do, is combine the date and time fields
SELECT * FROM mytable
WHERE
concat(fecha,' ',horain) as date BETWEEN '2012-05-07 07:00:00' AND '2012-05-08 06:59:59';
Related
I'm working on a project for a Fuel Pump. Here is my table for the records:
I want to display rows from very start (2019-07-03) till the end of the specific month (e.g 2019-09-29) . How could i do achieve this?
A simple WHERE clause will do the trick
SELECT id, date, total_amount, total_paid
FROM table
WHERE date <= LAST_DAY(CURDATE())
CURDATE() will return current date i.e. 2019-09-08
LAST_DAY() will return the last date of current month i.e. 2019-09-30
WHERE clause will return all rows with date <= 2019-09-30
Update
If you want to filter records based on user input which is month and year ( 2019-09 ) then either it can done by appending '-01' at scripting side or using CONCAT at MySQL level,
WHERE date <= LAST_DAY(CONCAT('2019-09', '-01'))
I think this will work. You can change dates accordingly.
Select *
From table
Where date>='2019-07-03' AND date<='2019-09-29'
I am trying to use TIMESTAMPDIFF function in one of my queries and is making an headache for me.
TABLE
id | web_address | timeperiod | timeperiod_exp
1 | www.google.com | 1564692614 | 1564779014
1564692614 = GMT: Thursday, August 1, 2019 8:50:14 PM
1564779014 = GMT: Friday, August 2, 2019 8:50:14 PM
WHATS THE PROBLEM ?
As one can see the difference between these two timestamps is exactly 1 day but is returning no records.
SELECT * FROM mytable WHERE TIMESTAMPDIFF(DAY, timeperiod, timeperiod_exp) >= 1
WHERE IS THE FIDDLE ?
https://www.db-fiddle.com/f/udmrC2xdvrEEKGxEF7ty84/7
WHAT SHOULD BE DONE ?
Please take a look at the fiddle above and suggest what should be modified or other function in place of timestampdiff.
Look at the documentation for TIMESTAMPDIFF()
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and
datetime_expr2 are date or datetime expressions. One expression may be
a date and the other a datetime
As you see, it expects the parameters to be of type DATE or DATETIME. But you have your timestamps stored as integers.
So either use FROM_UNIXTIME() to convert your integer timestamps to DATETIME:
SELECT *
FROM mytable
WHERE TIMESTAMPDIFF(DAY, FROM_UNIXTIME(timeperiod), FROM_UNIXTIME(timeperiod_exp)) >= 1
db-fiddle
Or just use simple arithmetics (since we know how many seconds are in one day):
SELECT *
FROM mytable
WHERE (timeperiod_exp - timeperiod) >= 60*60*24
db-fiddle
As if i see the function TIMESTAMPDIFF() should take two timestamps but it is taking dates instead of direct timestamps in integers Thus the following works:
SELECT * FROM mytable WHERE TIMESTAMPDIFF(DAY, FROM_UNIXTIME(timeperiod), FROM_UNIXTIME(timeperiod_exp)) >= 1
Updated Fiddle
https://www.db-fiddle.com/f/udmrC2xdvrEEKGxEF7ty84/8
I have a database with the following structure and I am trying to get all rows from this table based on passing both the month and year using a where on the timestamp column (this will be a unix standard timestamp)
e.g month - 3, year - 2018 // get all rows for March 2018 only.
// db structure
id, timestamp, post_title
If you want rows for a given month using a unix timestamp, I would recommend:
where timestamp >= unix_timestamp('2018-03-01') and
timestamp < unix_timestamp('2018-04-01')
If you are passing in a variable, I would recommend passing in the first day of the month and doing:
where timestamp >= unix_timestamp(?) and
timestamp < unix_timestamp(? + interval 1 month)
Use convert function
SELECT * FROM dbo.YourTable WHERE CONVERT(VARCHAR(5),DATEPART(mm,timestamp))+'-'+CONVERT(VARCHAR(5),DATEPART(yy,timestamp)) ='3-2018'
I guess this can help you.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
I need to only SELECT data where the date field is yesterdays date. The only problem I'm having is that the data in the date field looks like the following 20160412 062815.000
I don't really care about the time, I just want to search dynamically for anything with yesterdays date. I've tried a multitude of CURDATE() -1 but I'm unsure how to just search the first 8 digits of the field.
Assuming the date value is stored as a string, and the first 8 characters are always the date in YYYYMMDD format, then you can use a query like this:
select *
from your_table
where your_column like concat(date_format(current_date() - interval 1 day,'%Y%m%d'),'%')
One advantage of this query is that it can leverage an index on your date field, unlike the other answers so far.
Format yesterday's date to a number and convert the date string also to a number.
select * from your_table
where date_format(curdate() - interval 1 day, '%Y%m%d') * 1 = date_col * 1
SQLFiddle demo
*1 is a math operation that forces MySQL to convert strings to a number.
you can use subdate(currentDate, 1)
select * from your_table
where subdate(currentDate, 1) = DATE(your_date)
We are using the following statement to select dates of birth which occur in the current week:
SELECT * FROM tbl_user WHERE WEEK(dob,1) = WEEK(CURDATE(),1) ORDER BY id DESC
Our 'dob' column has the type DATE and contains 1 record where dob is 1972-07-09. However, when we run our query (today is 2014-07-07) we get an empty result.
Any ideas why?? Does WEEK() only work on columns with type DATETIME?
Thanks in advance for an help!
SELECT WEEK('1972-07-09',1); //result= 27
SELECT WEEK('2014-07-07',1); //result=28
For Example :
SELECT WEEK('2014-01-18',1); //result=3
Your where condition does not satisfy that why It's return false and Empty Result.
Check Manual here
2014-07-07 is week 28 and 1972-07-09 is week 27. 27 is not equals 28 so you get no result.
You can check this with that query:
select week('2014-07-07',1),week('1972-07-09',1) from tbl_user ;
SELECT WEEK('1972-07-09',1);
result: 27
SELECT WEEK('2014-07-07',1);
result: 28
In this case your condition WHERE WEEK(dob,1) = WEEK(CURDATE(),1) is False. That's why you get an empty result.
Your definition of week is not the same as MySQLs. For a better and more efficient result you should instead decide on the min and max range you wish to find and select that which allows you to define when your week starts and ends.
SELECT *
FROM tbl_user
-- min range == 00:00:00 on the first day of week
WHERE dob >= ADDDATE(CURRENT_DATE, INTERVAL 1-DAYOFWEEK(CURRENT_DATE) DAY)
-- max range == 00:00:00 on the first day of next week
AND dob < ADDDATE(CURRENT_DATE, INTERVAL 8-DAYOFWEEK(CURRENT_DATE) DAY)
ORDER BY id DESC
The start and end date can be adjusted as needed, but because you aren't applying a function to the dob field you will avoid a table scan.