sql error using between operator with date between two dates - mysql

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).

Related

Mysql date select from string

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"

MySQL query to find most sold products within a time period

I have two tables. Here are some fields that will be used in query.
tgc_sales (code,sale_date_time, order_status)
tgc_sales_items (sales_code, quantity, product_name, product_code)
What I want is to get the most sold products within a specific period of time (say a week, a month etc).
What I tried so far:
SELECT SUM(tsi.quantity) AS quantity, tsi.product_name AS product_name
FROM tgc_sales_items tsi
JOIN tgc_sales ts ON ts.code = tsi.sales_code
WHERE DATE(ts.sale_date_time)>='2014-05-01'
AND ts.order_status='Completed'
GROUP BY tsi.product_code
ORDER BY quantity DESC LIMIT 10
Obviously, the query is wrong and it is giving me unexpected result. When I ignore the JOIN and WHERE clause it shows me the most sold products but I need it for when the sales was made in a specific period and I can't figure out how to do it.
The query looks right, assuming that code is a unique (or primary) key on the tgc_sales table, and sales_code is a foreign key reference to that column.
The use of the DATE() function seems a bit odd.
If the sales_date_time column has a datatype of DATE, DATETIME, or TIMESTAMP, then the DATE() function isn't needed, and it's not desirable because it disables MySQL's ability to use an index range scan to satisfy the predicate.
If the sales_date_time is character, and your intent is to convert the character into a DATE, you'd use the STR_TO_DATE() function. But you don't really want to store sales_date_time as a character string.
If that's a DATETIME column, you'd do something like this:
WHERE ts.sale_date_time >= '2014-05-01'
AND ts.sale_date_time < '2014-06-01'
If it's a character column in a non-canonical format (e.g. 'mm/dd/yyyy hh:mi:ss'), then you could do something like:
WHERE STR_TO_DATE(ts.sale_date_time,'%m/%d/%Y %h:%i:%s') >= '2014-05-01'
AND STR_TO_DATE(ts.sale_date_time,'%m/%d/%Y %h:%i:%s') < '2014-06-01'
(But you don't really want to store a date time value in a character string; you want to use one of the MySQL datatypes like DATETIME.)
If you're storing a unix-style timestamp "seconds since the beginning of an era", then you'd want to do the comparison against the native column values. You could do something like this:
WHERE ts.sale_date_time >= UNIX_TIMESTAMP('2014-05-01')
AND ts.sale_date_time < UNIX_TIMESTAMP('2014-06-01')
...though you'd really prefer to use the same library used to do the conversion when the values were stored, and do the query more like this:
WHERE ts.sale_date_time >= 1398902400
AND ts.sale_date_time < 1401580800
Your group by seems suspicious. It includes product_code column which is not in the select clause.

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')

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.

Mysql Date manipulation

I have a table with different dates and one of them is separated in 3 fields:
PassDay, PassMonth, PassYear.
I want to compare these dates with an other datetime orig_date so what I did is
select `orig_date`, CONCAT_WS('-',`PassYear`,`PassMonth`,`PassDay`) as `pass_expiration` where `pass_expiration` < `orig_date`;
The problem is that the month is stored as a month name (January) and not a number (01), so my result for pass_expiration is 2013-April-15 for example.
Is there a way to transform this (2013-April-15) to a regular datetime directly from the query?
You want str_to_date:
where STR_TO_DATE(CONCAT_WS('-',PassYear,PassMonth,PassDay),'%Y-%M-%d') < orig_date
Also, as a note, you can't reference a computed column in the where clause. Only in the order by clause (where is a predicate that's used to filter results from the table; order by is run after the column set is calc'd).