mysql: between not working with date
up vote
2
down vote
favorite
I am Writing a Between query with formatted date..
This is my query:
SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')
However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.
If anyone can find anything out of this, please tell me..
One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.
mysql date between
add a comment |
up vote
2
down vote
favorite
I am Writing a Between query with formatted date..
This is my query:
SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')
However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.
If anyone can find anything out of this, please tell me..
One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.
mysql date between
1
You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15
I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am Writing a Between query with formatted date..
This is my query:
SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')
However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.
If anyone can find anything out of this, please tell me..
One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.
mysql date between
I am Writing a Between query with formatted date..
This is my query:
SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')
However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.
If anyone can find anything out of this, please tell me..
One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.
mysql date between
mysql date between
asked Apr 20 '16 at 19:36
Archit
747
747
1
You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15
I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44
add a comment |
1
You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15
I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44
1
1
You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15
You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15
I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44
I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).
You need to use real DATE values for BETWEEN to work properly.
If the columns are already
DATEcolumns, just skip the formatting:
SELECT shop_id, registered_time FROM shops where registered_time
BETWEEN '2016-03-09' AND '2016-04-19'
If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.
add a comment |
up vote
1
down vote
You need to turn your strings back into dates that MySQL understands. From the documentation:
CAST(datetime_col AS DATE)
should help accomplish what you want.
Reference
1
CAST()is unlikely to understand theDD-MM-YYYYformat though.
– Pekka 웃
Apr 20 '16 at 19:42
add a comment |
up vote
-1
down vote
Do this:
select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');
I had the same issue, then I tried this and it worked. Good Luck!
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).
You need to use real DATE values for BETWEEN to work properly.
If the columns are already
DATEcolumns, just skip the formatting:
SELECT shop_id, registered_time FROM shops where registered_time
BETWEEN '2016-03-09' AND '2016-04-19'
If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.
add a comment |
up vote
2
down vote
You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).
You need to use real DATE values for BETWEEN to work properly.
If the columns are already
DATEcolumns, just skip the formatting:
SELECT shop_id, registered_time FROM shops where registered_time
BETWEEN '2016-03-09' AND '2016-04-19'
If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.
add a comment |
up vote
2
down vote
up vote
2
down vote
You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).
You need to use real DATE values for BETWEEN to work properly.
If the columns are already
DATEcolumns, just skip the formatting:
SELECT shop_id, registered_time FROM shops where registered_time
BETWEEN '2016-03-09' AND '2016-04-19'
If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.
You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).
You need to use real DATE values for BETWEEN to work properly.
If the columns are already
DATEcolumns, just skip the formatting:
SELECT shop_id, registered_time FROM shops where registered_time
BETWEEN '2016-03-09' AND '2016-04-19'
If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.
edited May 23 '17 at 11:54
Community♦
11
11
answered Apr 20 '16 at 19:40
Pekka 웃
352k1138361009
352k1138361009
add a comment |
add a comment |
up vote
1
down vote
You need to turn your strings back into dates that MySQL understands. From the documentation:
CAST(datetime_col AS DATE)
should help accomplish what you want.
Reference
1
CAST()is unlikely to understand theDD-MM-YYYYformat though.
– Pekka 웃
Apr 20 '16 at 19:42
add a comment |
up vote
1
down vote
You need to turn your strings back into dates that MySQL understands. From the documentation:
CAST(datetime_col AS DATE)
should help accomplish what you want.
Reference
1
CAST()is unlikely to understand theDD-MM-YYYYformat though.
– Pekka 웃
Apr 20 '16 at 19:42
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to turn your strings back into dates that MySQL understands. From the documentation:
CAST(datetime_col AS DATE)
should help accomplish what you want.
Reference
You need to turn your strings back into dates that MySQL understands. From the documentation:
CAST(datetime_col AS DATE)
should help accomplish what you want.
Reference
answered Apr 20 '16 at 19:41
Jacob See
715617
715617
1
CAST()is unlikely to understand theDD-MM-YYYYformat though.
– Pekka 웃
Apr 20 '16 at 19:42
add a comment |
1
CAST()is unlikely to understand theDD-MM-YYYYformat though.
– Pekka 웃
Apr 20 '16 at 19:42
1
1
CAST() is unlikely to understand the DD-MM-YYYY format though.– Pekka 웃
Apr 20 '16 at 19:42
CAST() is unlikely to understand the DD-MM-YYYY format though.– Pekka 웃
Apr 20 '16 at 19:42
add a comment |
up vote
-1
down vote
Do this:
select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');
I had the same issue, then I tried this and it worked. Good Luck!
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
add a comment |
up vote
-1
down vote
Do this:
select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');
I had the same issue, then I tried this and it worked. Good Luck!
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Do this:
select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');
I had the same issue, then I tried this and it worked. Good Luck!
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Do this:
select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');
I had the same issue, then I tried this and it worked. Good Luck!
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
Marcel Stör
14k447122
14k447122
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered yesterday
M IMRAN FARUQI
11
11
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
add a comment |
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
Please replace '2018-11-10' with your desired date!
– M IMRAN FARUQI
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36753530%2fmysql-between-not-working-with-date%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15
I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44