I have the following sql query:
select date, apptemp from weather where apptemp = '20.1';
Empty set (0.05 sec)
If I then run:
select date, apptemp from weather where apptemp like '%20.1';
+---------------------+---------+
| date | apptemp |
+---------------------+---------+
| 2014-09-24 15:18:43 | 20.1 |
| 2014-09-24 16:23:41 | 20.1 |
| 2014-09-25 14:08:57 | 20.1 |
+---------------------+---------+
I thought it might be due to a space in front of the number but ' 20.1' or ' 20.1' does not yield results either.
How can I see what character is in front of the 20.1?
Update: Thanks - I've tried the HEX:
mysql> SELECT date, apptemp, HEX(apptemp) FROM weather;
huh?
+---------------------+---------+--------------+
| date | apptemp | HEX(apptemp) |
+---------------------+---------+--------------+
| 2014-09-25 14:19:04 | 21.4 | 15 |
| 2014-09-25 14:24:02 | 21.3 | 15 |
| 2014-09-25 14:28:57 | 20.8 | 15 |
| 2014-09-25 14:34:02 | 21.4 | 15 |
| 2014-09-25 14:38:59 | 21.2 | 15 |
+---------------------+---------+--------------+
Update 2:
Seems to be something with the . - if I do
select date, apptemp from weather where apptemp = '20';
It returns the correct data.
"20.1" - does not.
I'd use the HEX function:
SELECT HEX(apptemp) FROM weather;
This will show the column's value in hexadecimal. Key hex values to look for are 00 (ASCII NUL), 09 (tab), and 20 (space), though there are a host of other "invisible" values.
It's probably easier to query the column along with its hex value so you can see the plain and geek versions of the value:
SELECT apptemp, HEX(apptemp) FROM weather;
Thanks for the hex tip.
Seems its caused because the datatype is a float.
mysql float data not selecting in where clause
Set to decimal and its working now.
Related
I have a table
---------+-----------+------------------+--------------------+
| id | user_id | start_date_time | end_date_time |
+---------+---------+-------------------+--------------------+
| 1 | 11 |2019-11-17 20:10:00|2019-11-17 21:05:00 |
| 2 | 11 |2019-11-17 20:18:00|2019-11-17 20:35:00 |
| 3 | 11 |2019-11-17 20:32:00|2019-11-17 21:18:00 |
| 4 | 11 |2019-11-17 20:40:00|2019-11-17 20:50:00 |
| 5 | 11 |2019-11-17 20:45:00|2019-11-17 21:20:00 |
| | | | |
+---------+---------+-------------------+--------------------+
Scenario 1 - If i query for all greater than '2019-11-17 20:18:00' I need to get all records.
Scenario 2 - If i query for all possible dates greater or equals '2019-11-17 21:18:00' It should return record 3 and 5.
For any given time it should look for Start_date_time and End_date_time where given time should be considered as start time and it should look for appropriate end_date_time and output the result.
In a nut shell input time should be taken as starting range and it should look for End_date_time and give me all values between.
How can i accomplish this?
i tried the following ways on db-fiddle https://www.db-fiddle.com/f/bPk1CYioL6cVasStZKzQ4j/7
if i query all records from a given time eg(2019-11-17 20:18:00) the input should be taken as a start datetime of range and look for the most greatest end_date_time and give me the records between them. Example 2019-11-17 20:18:00 this input takes range between input as val1 of range 2019-11-17 20:18:00 to 2019-11-17 21:20:00 the highest end date and give me all records between. And if i query with input 2019-11-17 21:05:00 this should take start range val1 as 2019-11-17 21:05:00 and 2019-11-17 21:20:00 output 1,3,5 records.
The requirement (with the results that you expect) is as simple as that:
select *
from times
where ? <= end_date_time
Replace ? with the datetime that you want to query.
See the demo.
I have the following query:
SELECT `Time`,
`Resolution`,
HOUR(TIMEDIFF(`Resolution`,`Time`)),
TIMEDIFF(`Resolution`,`Time`),
datediff(`Resolution`,`Time`)
FROM Cases;
In order to debug, I add the TIMEDIFF without the HOUR before, just to see if the result is different. I use datediff to double check.
The result of the query is:
+---------------------+---------------------+-------------------------------------+-------------------------------+-------------------------------+
| Time | Resolution | HOUR(TIMEDIFF(`Resolution`,`Time`)) | TIMEDIFF(`Resolution`,`Time`) | datediff(`Resolution`,`Time`) |
+---------------------+---------------------+-------------------------------------+-------------------------------+-------------------------------+
| 2017-01-10 13:35:00 | 2017-01-24 10:52:00 | 333 | 333:17:00 | 14 |
| 2017-01-12 15:53:00 | 2017-02-21 16:06:00 | 838 | 838:59:59 | 40 |
| 2017-01-18 09:19:00 | 2017-01-18 13:39:00 | 4 | 04:20:00 | 0 |
| 2017-01-23 09:00:00 | 2017-01-23 15:08:00 | 6 | 06:08:00 | 0 |
| 2017-01-24 08:49:00 | 2017-02-20 14:34:00 | 653 | 653:45:00 | 27 |
Actually, it delivers more lines, but the relevant line is the 2 result - 838 hours, which translates to 34.91 days, let's say 35, but the DATEDIFF give 40 and when you do yourself the calculation it is 40 days! 12th Jan to 21st Feb.
All other 21 results are correct.
Any idea why? A bug in mysql?
All responses are highly appreciated.
Use
TIMESTAMPDIFF(HOUR,`Time`, `Resolution`)
instead.
It also negates the need to use HOUR().
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
The result returned by TIMEDIFF() is limited to the range allowed for TIME values. https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
TIME values may range from -838:59:59 to 838:59:59. https://dev.mysql.com/doc/refman/5.5/en/time.html
So you're getting the maximum possible value.
I'm creating a database (in MySQL) with a table of measurements. For each measurement I want to store the DateTime it came in. For showing plots within an app for different intervals (measurements of the day/week/month/year) I want sample the data points I have, so I can return e. g. 30 data points for the whole year as well as for the day/hour. This is the same as done with stock price graphs:
stock price plot for 1 day
vs
stock price plot for 1 month
As you can see, the amount of data points is the same in both pictures.
So how can I select x entries within a timespan in MySQL via SQL?
My data looks like this:
+====+====================+=============+==========+
| id | datetime | temperature | humidity |
+====+====================+=============+==========+
| 1 | 1-15-2016 00:30:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 2 | 1-15-2016 00:35:00 | 19 | 41 |
+----+--------------------+-------------+----------+
| 3 | 1-15-2016 00:40:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 4 | 1-15-2016 00:45:00 | 20 | 42 |
+----+--------------------+-------------+----------+
| 5 | 1-15-2016 00:50:00 | 21 | 42 |
+----+--------------------+-------------+----------+
| 6 | 1-15-2016 00:55:00 | 20 | 43 |
+----+--------------------+-------------+----------+
| 7 | 1-15-2016 01:00:00 | 21 | 43 |
+====+====================+=============+==========+
Let's say, I always want two data points (in reality a lot more). So for the last half hour I want the database to return data point 1 and 4, for the last ten minutes I want it to return 6 and 7.
Thanks for helping!
PS: I'm sorry for any errors in my English
OK, assuming a very simple systematic approach, you can get the first and last entry for any defined period:
select *
from table
where mydatetime =
(select
max(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
OR mydatetime =
(select
min(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
I believe your answer can be found at the following location:
https://stackoverflow.com/a/1891796/7176046
If you are looking to filter out any items not within your date/time your query would use:
Select * from table where Date/Time is (What you want to sort by)
Need help to suggest sql query where I have a varchar column called Timing like below
+---------------------+
| Timing | MO |
+---------------------+
|7/11/2016 20:45 |ABC |
|7/12/2016 20:45 |ABC |
|2016-07-11 00:00|ABC |
|2016-07-12 00:00|ABC |
+---------------------+
I need to extract date from the Timing column but tricky part is the date is not always in same format. I need to output timing column as below
+-------------+
| Timing | MO|
+-------------+
|7/11/2016|ABC|
|7/12/2016|ABC|
|7/11/2016|ABC|
|7/12/2016|ABC|
+-------------+
It's simple enough using substring and replace functions to cast varchar as data and recast them to you required output.
for example
DROP TABLE T;
CREATE TABLE T (TIMING VARCHAR(20),MO VARCHAR(2));
INSERT INTO T VALUES
('7/11/2016 20:45' ,'AB'),
('7/12/2016 20:45' ,'AB'),
('2016-07-11 00:00','AB'),
('2016-07-12 00:00','AB'),
('27/06/2016 20:45' ,'AB'),
('29/05/2016 20:45' ,'AB')
*/
SELECT *,
CASE
WHEN INSTR(TIMING,'/') = 0 THEN
CASE WHEN INSTR(TIMING,'-') = 5 THEN CAST(TIMING AS DATE)
end
WHEN INSTR(TIMING,'/') = 3 THEN
CAST(
CONCAT(
SUBSTRING(CONCAT('0',TIMING),8,4)
,'-'
,SUBSTRING(CONCAT('0',TIMING),5,2)
,'-'
,SUBSTRING(CONCAT('0',TIMING),1,2)
)
AS DATE
)
WHEN INSTR(TIMING,'/') = 2 THEN
CAST(
CONCAT(
SUBSTRING(CONCAT('0',TIMING),7,4)
,'-'
,SUBSTRING(CONCAT('0',TIMING),4,2)
,'-'
,SUBSTRING(CONCAT('0',TIMING),1,2)
)
AS DATE
)
END
FROM T
results in
-----------------------------------------------------------------------------+
| 7/11/2016 20:45 | AB | 2016-11-07 |
| 7/12/2016 20:45 | AB | 2016-12-07 |
| 2016-07-11 00:00 | AB | 2016-07-11 |
| 2016-07-12 00:00 | AB | 2016-07-12 |
| 27/06/2016 20:45 | AB | 2016-06-02 |
| 29/05/2016 20:45 | AB | 2016-05-02 |
+------------------+------+-----------------------------------------------------
You do have to code for every known variant - in this case I have identified that 7/11/2016 needs to left padded with zero and slashes replaced with dashs before date conversion. Hopefully you have a limited number of date variations in your source to code for and you will end up with a large query - but that's life. God luck,
I have the following table called seasons
+----+--------+------------+------------+
| id | cost | start | end |
+----+--------+------------+------------+
| 33 | 255 | 2014-01-05 | 2014-04-16 |
| 17 | 357 | 2014-04-17 | 2014-04-19 |
| 65 | 191.25 | 2014-04-20 | 2014-07-10 |
| 49 | 255 | 2014-07-11 | 2014-08-23 |
| 81 | 191.25 | 2014-08-24 | 2014-12-18 |
+----+--------+------------+------------+
I am trying to get a date range between start and end using the following query.
SELECT
*
FROM
seasons
WHERE
(start BETWEEN '2014-01-05' AND '2014-01-05' OR end BETWEEN '2014-01-05' AND '2014-01-05');
I was able to get a result set if the start date started exactly on the value on the field.
+----+------+------------+------------+
| id | cost | start | end |
+----+------+------------+------------+
| 33 | 255 | 2014-01-05 | 2014-04-16 |
+----+------+------------+------------+
Now the problem is when I advance the date to
2014-01-06
SELECT
*
FROM
seasons
WHERE
(start BETWEEN '2014-01-06' AND '2014-01-06' OR end BETWEEN '2014-01-06' AND '2014-01-06');
Empty set (0.00 sec)
There are NO RESULT. How can I get the date range in between to different fields on SQL?
Any help is appreciated.
You have your logic backward. If you want seasons where a given date is in that season then your where clause should look like:
WHERE '2014-01-06' BETWEEN start AND end;
Since you are not really looking for a range and only needing a specific date, you could also just use (SQL Fiddle):
SELECT *
FROM seasons
WHERE start = '2014-01-05' OR end = '2014-01-05'
When querying if a certain date is in a given range, the WHERE clause typically is:
WHERE 'specified_date' BETWEEN 'start_date' AND 'end_date'
Otherwise it is an illogical date span to MySQL and will return an empty result set.
The BETWEEN Comparison Operator is equivalent to:
min <= expr AND expr <= max
Therefore, it's meant to be used like this:
expr BETWEEN min AND max
Here is documentation from MySQL on the BETWEEN Comparison Operator.