Mysql date select from string - mysql

i've issue I try this query, do not return any rows. just 0 rows. Even tho there is data matching the request..
select * from repairshop_reservations where date = DATE_FORMAT("11/06/2017 20:00:00", '%d/%m/%Y %H:%i:%s"');
Currently my content of the selected table look like this
The data value of column Date is datetime

you could use str_to_date in this way you can control the proper formatting of the date when you don't use the standard mysql format
select * from repairshop_reservations
where date = str_to_date('11/06/2017 20:00:00', '%d/%m/%Y %H:%i:%s');

You are not inserting a column in your table, so you won't have to define a data type for it. That means that, you are not making changes to the conceptual scheme of your database.
Considering that your table is implemented correctly, the SQL query you would need to give you the desirable result would be:
SELECT * FROM repairshop_reservations
WHERE date = "11/06/2017 20:00:00";
You use the WHERE clause, to filter your record and get an output with a
specified condition. In plain English, what you want to do is:
Select and print for me, every column from the repairshop_reservations table, that has listed date as "11/06/2017 20:00:00"

Related

Optimize query with large number of data on mysql

I have a more than 10 million data from my table and needs to pull it in order to display in the report. The origin of data was extracted from csv and all of them are in text format. and here is how it looks from my table:
I tried to query with limit on 1000 only and it will display quickly however If I am going to have a date filter for e.g getting 1 day data and it will take around 25-30 secs:
SELECT STR_TO_DATE(SUBSTRING_INDEX(time, '_', 1), '%m/%d/%Y') FROM myTable
WHERE STR_TO_DATE(SUBSTRING_INDEX(time, '_', 1), '%m/%d/%Y') BETWEEN DATE('2019-9-3') AND DATE('2019-9-3');
I already tried to create an index on time column which I am using for filter but still got the same result:
Is there any suggestion/comments how can I improve the speed to pull the data. TIA
When you apply functions to a column as part of your search, it can't use an index, even if you define an index for that column.
You should also use a proper DATE or DATETIME data type for the column, which will require dates be stored in YYYY-MM-DD format, not a string column in MM/DD/YYYY format.
If you store the dates properly, you can do this:
SELECT DATE(time) FROM myTable
WHERE time >= '2019-09-03' AND time < '2019-09-04';
That will make use of the index.
You are storing your dates/timestamps as text, which is going to force you to doing suboptimal things like calling STR_TO_DATE all over the place. I suggest adding a new bona fide datetime column, and then indexing that column:
ALTER TABLE myTable ADD COLUMN time_dt DATETIME;
Then, populate it using STR_TO_DATE:
UPDATE myTable
SET time_dt = STR_TO_DATE(time, '%m/%d/%Y_%H:%i:%s.%f');
Then, add an index on time_dt:
CREATE INDEX idx ON myTable (time_dt);
And finally, rewrite your query so that the WHERE clause is sargable (i.e. so that it may use the above index):
SELECT DATE(time_dt)
FROM myTable
WHERE time_dt >= '2019-09-03' AND time_dt < '2019-09-04';
Side note: You need to use %H in the format mask with STR_TO_DATE, because your hours are in 24-hour clock mode.

How to Query between a date range in MySQL

I'm querying
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '02-04-2017'
and the result is null. How to fix this. But
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '30-03-2017'
giving me the correct result.
Note:- tempLog is the table name.
You should store dates in date format or atleast correctly formatted string (YYYY-MM-DD).
For now you can use str_to_date to convert the string to date and compare:
select *
from tempLog
where str_to_date(date, '%d-%m-%Y') between '2017-03-23' and '2017-04-02';
However note that this will hinder the optimizer from using index on the column if any.
The correct remedy of the situation is fixing the table structure.
According to the documentation, you're supposed to use this format when writing a date: 'YYYY-MM-DD' (although it says it may accept 'YYYYMMDD' or even YYYYMMDD in some contexts).

sql error using between operator with date between two dates

i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.
eg :
SELECT *
FROM `events`
WHERE '2017-01-29' BETWEEN start_date='2017-01-28'
AND end_date='2017-01-31'
but response is syntax error can any one help me to finish the query
Just list the columns.
WHERE '2017-01-29' BETWEEN start_date AND end_date
The values come from the table, you don't put them into the query.
According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is
expr BETWEEN min AND max
it is not
expr BETWEEN blabla=min AND stuff=max
Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.
It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like
WHERE '2017-01-29' BETWEEN start_date
AND end_date
(assuming start_date and end_date are columns in your table)
or something like
WHERE some_column BETWEEN '2017-01-28'
AND '2017-01-31'
(assuming some_column is a column in your table.)
I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:
SELECT *
FROM `events`
WHERE
date = '2017-01-29';
If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:
SELECT *
FROM `events`
WHERE
date BETWEEN '2017-01-28' AND '2017-01-31';
Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).

Query for rows within a date range that also have a specified substring

I'm trying to query a MySQL database and return all records within a given date range and which also contain the substring 'bank' in the content column.
The format of the 'time' field I refer to is mm/dd/yyyy hh:mm:ss.
Here's the statement I've come up with but MySQL Workbench is giving me issues:
SELECT *
FROM blogs
WHERE ((‘time’ BETWEEN “04/01/2011 00:00:00” AND “04/15/2011 23:59:59”)
AND (‘content’ LIKE ‘%bank%’))
How about trying this:
SELECT *
FROM blogs
WHERE `time` BETWEEN '2011-04-01 00:00:00' AND '2011-04-15 23:59:59'
AND `content` LIKE '%bank%';
This works if your time field is in fact a timestamp. If time is not a timestamp then you will have to go with something like the answer from McAdam331 but I'm hoping your database is using the correct types for the kind of data you are asking it to store.
single ' or double " quotes around values and ticks ` around field names. I also changed the date format to yyyy-mm-dd hh:mm:ss and eliminated some unnecessary parentheses.
http://sqlfiddle.com/#!9/730bd/1/0
It would be helpful if you posted the structure of the table when posting questions like this so we can be sure to give the right answer.
It isn't a good idea to store dates like that in MySQL. The DBMS has Date and Time Types you can use to store that information.
If changing the database isn't an option, you can convert a string to a date object using the STR_TO_DATE function, which takes in a date string and the format that it is in already and returns a date.
MySQL stores dates in the 'YYYY-MM-DD' format, so to get that format you can try something like this:
SELECT STR_TO_DATE('04/01/2011', '%m/%d/%Y');
Which will return a date object for that day. Note the capital Y.
Then it becomes much easier to query between dates, like this:
SELECT *
FROM myTable
WHERE STR_TO_DATE(dateString, '%m/%d/%Y %H:%i:%s') BETWEEN STR_TO_DATE('04/01/2011 00:00:00', '%m/%d/%Y %H:%i:%s') AND STR_TO_DATE('04/15/2011 23:59:59', '%m/%d/%Y %H:%i:%s')
AND content LIKE '%bank%';
Here is an SQL Fiddle example, and here is a link that has the formatting characters you need.

SQL Server: Want to use between clause with dates, but dates in string form (YYYY.MM.DD)

Help! One column in my database is for dates. All of my dates are unfortunately in the String form (YYYY.MM.DD). I have a MASSIVE database (300+GB) so ideally would like to avoid transformations.
Is there a way I can select rows for dates in between YYYY.MM.DD and YYYY.MM.DD? What would the script look like?
Thank you!
If the months and days are stored with leading zeroes, the BETWEEN operator will work as expected. So will ORDER BY.
create table your_table (
date_value varchar(10) not null
);
insert into your_table values
('2013.01.01'), ('2013.01.13'), ('2013.01.30'), ('2013.01.31'),
('2013.02.01'), ('2013.02.13'), ('2013.02.28'), ('2013.02.31'),
('2013.03.01'), ('2013.03.15'), ('2013.03.30'), ('2013.03.31');
select date_value
from your_table
where date_value between '2013.01.01' and '2013-01-31'
order by date_value;
2013.01.01
2013.01.13
2013.01.30
One of the main problems with your structure is that you lose type safety. Look at this query.
select date_value
from your_table
where date_value between '2013.02.01' and '2013.02.31'
order by date_value;
2013.02.01
2013.02.13
2013.02.28
2013.02.31
If you'd used a column of type date or datetime or timestamp, the dbms would not have allowed inserting the values '2013.02.31', because that's not a value in the domain of date. It is a value in the domain of varchar. (And so is "Arrrrgh!", unless you've got a CHECK constraint on that column that severely restricts the acceptable values.)
Not good solution, but works (cost much performance).
You have formated date in order year, month, day (good order to compare strings, without transformation to datetime), so you can try
SELECT * FROM Table WHERE StringDate > '2013.07.10' AND StringDate < '2013.07.14'
It returns bad results if there are dates before year 1000 without leading zero ('999.07.14').
But I dont know how it works on big database.
SQL Fiddle
Between in SQL is inclusive of both bounds. If that is what you want, you can just use between:
where col between 'YYYY.MM.DD' and 'YYYY.MM.DD'
Where the two constants are whatever values you are looking for.
If you have an index on the column, then between (as well as >, >=, and so on) will use the index. You do not need to transform the values. If your constants are dates of one form or another, then you can use date_format() to create a string in the right format. For instance, to get dates within the past week:
where col >= date_format(adddate(now(), -7), '%Y.%m.%d')