How select all values between 2 date by year, month, day columns?












1















In MySQL database, I have table with YEAR, MONTH, DAY columns.



| YEAR | MONTH | DAY | VALUE |
|----------------------------|
| 2018 | 11 | 9 | 1056 |
| 2018 | 11 | 10 | 6582 |
|****************************|
| 2018 | 12 | 9 | 6892 |
| 2018 | 12 | 10 | 5987 |
|****************************|
| 2019 | 3 | 5 | 5693 |
| 2019 | 3 | 6 | 5698 |


I need to take all values from the table between 2 date.



Let's say between 2018-11-09 and 2019-03-05 or between 2018-11-10 and 2018-12-09.



I need to say that unfortunately I can't merge these three column for one datetime column. Also the table has partitioning by that 3 columns.



Datatype of columns: smallint(6)










share|improve this question




















  • 2





    Save yourself a world of pain and store dates using a proper data type

    – Strawberry
    Nov 24 '18 at 8:36











  • Why is DAY sometimes 9 and sometimes padded with zero, 05 . What is the datatype of these three columns ?

    – Madhur Bhaiya
    Nov 24 '18 at 8:50











  • Don't want to go into all details, but there are a lot of reasons why I can't make it. I know that it's more easy to use BETWEEN operator for one datetime column, but question here is not about that.

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:51











  • @MadhurBhaiya it's my fault. In fact, it must be just 5 instead of 05. There is no zero. I edit my post. You can check it again. The type of these columns are smallint(6)

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:55













  • See CONCAT - but honestly, a better idea would be to see that whoever constructed this schema gets fired, and then you're free to redesign it.

    – Strawberry
    Nov 24 '18 at 9:12
















1















In MySQL database, I have table with YEAR, MONTH, DAY columns.



| YEAR | MONTH | DAY | VALUE |
|----------------------------|
| 2018 | 11 | 9 | 1056 |
| 2018 | 11 | 10 | 6582 |
|****************************|
| 2018 | 12 | 9 | 6892 |
| 2018 | 12 | 10 | 5987 |
|****************************|
| 2019 | 3 | 5 | 5693 |
| 2019 | 3 | 6 | 5698 |


I need to take all values from the table between 2 date.



Let's say between 2018-11-09 and 2019-03-05 or between 2018-11-10 and 2018-12-09.



I need to say that unfortunately I can't merge these three column for one datetime column. Also the table has partitioning by that 3 columns.



Datatype of columns: smallint(6)










share|improve this question




















  • 2





    Save yourself a world of pain and store dates using a proper data type

    – Strawberry
    Nov 24 '18 at 8:36











  • Why is DAY sometimes 9 and sometimes padded with zero, 05 . What is the datatype of these three columns ?

    – Madhur Bhaiya
    Nov 24 '18 at 8:50











  • Don't want to go into all details, but there are a lot of reasons why I can't make it. I know that it's more easy to use BETWEEN operator for one datetime column, but question here is not about that.

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:51











  • @MadhurBhaiya it's my fault. In fact, it must be just 5 instead of 05. There is no zero. I edit my post. You can check it again. The type of these columns are smallint(6)

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:55













  • See CONCAT - but honestly, a better idea would be to see that whoever constructed this schema gets fired, and then you're free to redesign it.

    – Strawberry
    Nov 24 '18 at 9:12














1












1








1








In MySQL database, I have table with YEAR, MONTH, DAY columns.



| YEAR | MONTH | DAY | VALUE |
|----------------------------|
| 2018 | 11 | 9 | 1056 |
| 2018 | 11 | 10 | 6582 |
|****************************|
| 2018 | 12 | 9 | 6892 |
| 2018 | 12 | 10 | 5987 |
|****************************|
| 2019 | 3 | 5 | 5693 |
| 2019 | 3 | 6 | 5698 |


I need to take all values from the table between 2 date.



Let's say between 2018-11-09 and 2019-03-05 or between 2018-11-10 and 2018-12-09.



I need to say that unfortunately I can't merge these three column for one datetime column. Also the table has partitioning by that 3 columns.



Datatype of columns: smallint(6)










share|improve this question
















In MySQL database, I have table with YEAR, MONTH, DAY columns.



| YEAR | MONTH | DAY | VALUE |
|----------------------------|
| 2018 | 11 | 9 | 1056 |
| 2018 | 11 | 10 | 6582 |
|****************************|
| 2018 | 12 | 9 | 6892 |
| 2018 | 12 | 10 | 5987 |
|****************************|
| 2019 | 3 | 5 | 5693 |
| 2019 | 3 | 6 | 5698 |


I need to take all values from the table between 2 date.



Let's say between 2018-11-09 and 2019-03-05 or between 2018-11-10 and 2018-12-09.



I need to say that unfortunately I can't merge these three column for one datetime column. Also the table has partitioning by that 3 columns.



Datatype of columns: smallint(6)







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 9 '18 at 9:29









marc_s

577k12911141259




577k12911141259










asked Nov 24 '18 at 8:34









Nurzhan NogerbekNurzhan Nogerbek

88321942




88321942








  • 2





    Save yourself a world of pain and store dates using a proper data type

    – Strawberry
    Nov 24 '18 at 8:36











  • Why is DAY sometimes 9 and sometimes padded with zero, 05 . What is the datatype of these three columns ?

    – Madhur Bhaiya
    Nov 24 '18 at 8:50











  • Don't want to go into all details, but there are a lot of reasons why I can't make it. I know that it's more easy to use BETWEEN operator for one datetime column, but question here is not about that.

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:51











  • @MadhurBhaiya it's my fault. In fact, it must be just 5 instead of 05. There is no zero. I edit my post. You can check it again. The type of these columns are smallint(6)

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:55













  • See CONCAT - but honestly, a better idea would be to see that whoever constructed this schema gets fired, and then you're free to redesign it.

    – Strawberry
    Nov 24 '18 at 9:12














  • 2





    Save yourself a world of pain and store dates using a proper data type

    – Strawberry
    Nov 24 '18 at 8:36











  • Why is DAY sometimes 9 and sometimes padded with zero, 05 . What is the datatype of these three columns ?

    – Madhur Bhaiya
    Nov 24 '18 at 8:50











  • Don't want to go into all details, but there are a lot of reasons why I can't make it. I know that it's more easy to use BETWEEN operator for one datetime column, but question here is not about that.

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:51











  • @MadhurBhaiya it's my fault. In fact, it must be just 5 instead of 05. There is no zero. I edit my post. You can check it again. The type of these columns are smallint(6)

    – Nurzhan Nogerbek
    Nov 24 '18 at 8:55













  • See CONCAT - but honestly, a better idea would be to see that whoever constructed this schema gets fired, and then you're free to redesign it.

    – Strawberry
    Nov 24 '18 at 9:12








2




2





Save yourself a world of pain and store dates using a proper data type

– Strawberry
Nov 24 '18 at 8:36





Save yourself a world of pain and store dates using a proper data type

– Strawberry
Nov 24 '18 at 8:36













Why is DAY sometimes 9 and sometimes padded with zero, 05 . What is the datatype of these three columns ?

– Madhur Bhaiya
Nov 24 '18 at 8:50





Why is DAY sometimes 9 and sometimes padded with zero, 05 . What is the datatype of these three columns ?

– Madhur Bhaiya
Nov 24 '18 at 8:50













Don't want to go into all details, but there are a lot of reasons why I can't make it. I know that it's more easy to use BETWEEN operator for one datetime column, but question here is not about that.

– Nurzhan Nogerbek
Nov 24 '18 at 8:51





Don't want to go into all details, but there are a lot of reasons why I can't make it. I know that it's more easy to use BETWEEN operator for one datetime column, but question here is not about that.

– Nurzhan Nogerbek
Nov 24 '18 at 8:51













@MadhurBhaiya it's my fault. In fact, it must be just 5 instead of 05. There is no zero. I edit my post. You can check it again. The type of these columns are smallint(6)

– Nurzhan Nogerbek
Nov 24 '18 at 8:55







@MadhurBhaiya it's my fault. In fact, it must be just 5 instead of 05. There is no zero. I edit my post. You can check it again. The type of these columns are smallint(6)

– Nurzhan Nogerbek
Nov 24 '18 at 8:55















See CONCAT - but honestly, a better idea would be to see that whoever constructed this schema gets fired, and then you're free to redesign it.

– Strawberry
Nov 24 '18 at 9:12





See CONCAT - but honestly, a better idea would be to see that whoever constructed this schema gets fired, and then you're free to redesign it.

– Strawberry
Nov 24 '18 at 9:12












2 Answers
2






active

oldest

votes


















2














It seems this query should give you the results you want. It creates a date string out of your 3 columns and then uses STR_TO_DATE to convert that into a value that MySQL can compare with the search date strings.



SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT_WS('/', `DAY`, `MONTH`, `YEAR`), '%d/%m/%Y') BETWEEN '2018-11-09' AND '2019-03-05'


Output



YEAR    MONTH   DAY     VALUE
2018 11 9 1056
2018 11 10 6582
2018 12 9 6892
2018 12 10 5987
2019 3 5 5693


Demo on dbfiddle






share|improve this answer


























  • Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:08



















2














One way is to create a string in MySQL Date format (YYYY-MM-DD) using string functions such as Concat() and Lpad():



SELECT *
FROM your_table
WHERE CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0'))
BETWEEN '2018-11-09' AND '2019-03-05'




Based on further discussion in comments, if you can input the year, month, and day value separately for the given data range(s); instead of creating a date using functions, we can directly use the respective columns instead. This will also allow us to utilize indexes (if defined) on these columns.



SELECT *
FROM your_table
WHERE
/* Condition for the month in the start date of the range */
(YEAR = 2018 AND MONTH = 11 AND DAY >= 9)
OR
/* Condition for the rest of the months in start date year */
(YEAR = 2018 AND MONTH > 11)
OR
/* Condition for the month in the end date of the range */
(YEAR = 2019 AND MONTH = 3 AND DAY <= 5)
OR
/* Condition for the rest of the months in end date year */
(YEAR = 2019 AND MONTH < 3)
OR
/* Condition for the years between the start and end date */
(YEAR > 2018 AND YEAR < 2019)


Above mentioned conditions can be compressed further. But I have written in this manner, for ease of understand-ability.





However, it is recommended to create another column to store the date in Date format. If you cannot make changes to the application code, and if your MySQL version >= 5.7, you can look at Generated Columns, and refer to that in your SELECT query instead.



ALTER TABLE your_table
ADD COLUMN date_col DATE
GENERATED ALWAYS AS CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0')) STORED;


Then, the SELECT query becomes trivial:



SELECT * FROM your_table
WHERE date_col BETWEEN '2018-11-09' AND '2019-03-05'





share|improve this answer


























  • Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:24











  • @NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

    – Madhur Bhaiya
    Nov 24 '18 at 20:34











  • Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

    – Nurzhan Nogerbek
    Nov 24 '18 at 21:04











  • @NurzhanNogerbek check the updated answer now.

    – Madhur Bhaiya
    Nov 24 '18 at 21:29











  • @NurzhanNogerbek any updates ? Does it work or not ?

    – Madhur Bhaiya
    Nov 25 '18 at 16:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456520%2fhow-select-all-values-between-2-date-by-year-month-day-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














It seems this query should give you the results you want. It creates a date string out of your 3 columns and then uses STR_TO_DATE to convert that into a value that MySQL can compare with the search date strings.



SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT_WS('/', `DAY`, `MONTH`, `YEAR`), '%d/%m/%Y') BETWEEN '2018-11-09' AND '2019-03-05'


Output



YEAR    MONTH   DAY     VALUE
2018 11 9 1056
2018 11 10 6582
2018 12 9 6892
2018 12 10 5987
2019 3 5 5693


Demo on dbfiddle






share|improve this answer


























  • Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:08
















2














It seems this query should give you the results you want. It creates a date string out of your 3 columns and then uses STR_TO_DATE to convert that into a value that MySQL can compare with the search date strings.



SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT_WS('/', `DAY`, `MONTH`, `YEAR`), '%d/%m/%Y') BETWEEN '2018-11-09' AND '2019-03-05'


Output



YEAR    MONTH   DAY     VALUE
2018 11 9 1056
2018 11 10 6582
2018 12 9 6892
2018 12 10 5987
2019 3 5 5693


Demo on dbfiddle






share|improve this answer


























  • Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:08














2












2








2







It seems this query should give you the results you want. It creates a date string out of your 3 columns and then uses STR_TO_DATE to convert that into a value that MySQL can compare with the search date strings.



SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT_WS('/', `DAY`, `MONTH`, `YEAR`), '%d/%m/%Y') BETWEEN '2018-11-09' AND '2019-03-05'


Output



YEAR    MONTH   DAY     VALUE
2018 11 9 1056
2018 11 10 6582
2018 12 9 6892
2018 12 10 5987
2019 3 5 5693


Demo on dbfiddle






share|improve this answer















It seems this query should give you the results you want. It creates a date string out of your 3 columns and then uses STR_TO_DATE to convert that into a value that MySQL can compare with the search date strings.



SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT_WS('/', `DAY`, `MONTH`, `YEAR`), '%d/%m/%Y') BETWEEN '2018-11-09' AND '2019-03-05'


Output



YEAR    MONTH   DAY     VALUE
2018 11 9 1056
2018 11 10 6582
2018 12 9 6892
2018 12 10 5987
2019 3 5 5693


Demo on dbfiddle







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 11:28

























answered Nov 24 '18 at 9:10









NickNick

30.4k121941




30.4k121941













  • Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:08



















  • Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:08

















Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

– Nurzhan Nogerbek
Nov 24 '18 at 20:08





Thank you for your answer! Your code return result but the problem is speed. For example weekly slice was processed in 25 seconds which is too long I think. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. In fact in my real table many records with the same day, month, year. The difference only in values column. Do you have any ideas about that?

– Nurzhan Nogerbek
Nov 24 '18 at 20:08













2














One way is to create a string in MySQL Date format (YYYY-MM-DD) using string functions such as Concat() and Lpad():



SELECT *
FROM your_table
WHERE CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0'))
BETWEEN '2018-11-09' AND '2019-03-05'




Based on further discussion in comments, if you can input the year, month, and day value separately for the given data range(s); instead of creating a date using functions, we can directly use the respective columns instead. This will also allow us to utilize indexes (if defined) on these columns.



SELECT *
FROM your_table
WHERE
/* Condition for the month in the start date of the range */
(YEAR = 2018 AND MONTH = 11 AND DAY >= 9)
OR
/* Condition for the rest of the months in start date year */
(YEAR = 2018 AND MONTH > 11)
OR
/* Condition for the month in the end date of the range */
(YEAR = 2019 AND MONTH = 3 AND DAY <= 5)
OR
/* Condition for the rest of the months in end date year */
(YEAR = 2019 AND MONTH < 3)
OR
/* Condition for the years between the start and end date */
(YEAR > 2018 AND YEAR < 2019)


Above mentioned conditions can be compressed further. But I have written in this manner, for ease of understand-ability.





However, it is recommended to create another column to store the date in Date format. If you cannot make changes to the application code, and if your MySQL version >= 5.7, you can look at Generated Columns, and refer to that in your SELECT query instead.



ALTER TABLE your_table
ADD COLUMN date_col DATE
GENERATED ALWAYS AS CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0')) STORED;


Then, the SELECT query becomes trivial:



SELECT * FROM your_table
WHERE date_col BETWEEN '2018-11-09' AND '2019-03-05'





share|improve this answer


























  • Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:24











  • @NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

    – Madhur Bhaiya
    Nov 24 '18 at 20:34











  • Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

    – Nurzhan Nogerbek
    Nov 24 '18 at 21:04











  • @NurzhanNogerbek check the updated answer now.

    – Madhur Bhaiya
    Nov 24 '18 at 21:29











  • @NurzhanNogerbek any updates ? Does it work or not ?

    – Madhur Bhaiya
    Nov 25 '18 at 16:28
















2














One way is to create a string in MySQL Date format (YYYY-MM-DD) using string functions such as Concat() and Lpad():



SELECT *
FROM your_table
WHERE CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0'))
BETWEEN '2018-11-09' AND '2019-03-05'




Based on further discussion in comments, if you can input the year, month, and day value separately for the given data range(s); instead of creating a date using functions, we can directly use the respective columns instead. This will also allow us to utilize indexes (if defined) on these columns.



SELECT *
FROM your_table
WHERE
/* Condition for the month in the start date of the range */
(YEAR = 2018 AND MONTH = 11 AND DAY >= 9)
OR
/* Condition for the rest of the months in start date year */
(YEAR = 2018 AND MONTH > 11)
OR
/* Condition for the month in the end date of the range */
(YEAR = 2019 AND MONTH = 3 AND DAY <= 5)
OR
/* Condition for the rest of the months in end date year */
(YEAR = 2019 AND MONTH < 3)
OR
/* Condition for the years between the start and end date */
(YEAR > 2018 AND YEAR < 2019)


Above mentioned conditions can be compressed further. But I have written in this manner, for ease of understand-ability.





However, it is recommended to create another column to store the date in Date format. If you cannot make changes to the application code, and if your MySQL version >= 5.7, you can look at Generated Columns, and refer to that in your SELECT query instead.



ALTER TABLE your_table
ADD COLUMN date_col DATE
GENERATED ALWAYS AS CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0')) STORED;


Then, the SELECT query becomes trivial:



SELECT * FROM your_table
WHERE date_col BETWEEN '2018-11-09' AND '2019-03-05'





share|improve this answer


























  • Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:24











  • @NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

    – Madhur Bhaiya
    Nov 24 '18 at 20:34











  • Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

    – Nurzhan Nogerbek
    Nov 24 '18 at 21:04











  • @NurzhanNogerbek check the updated answer now.

    – Madhur Bhaiya
    Nov 24 '18 at 21:29











  • @NurzhanNogerbek any updates ? Does it work or not ?

    – Madhur Bhaiya
    Nov 25 '18 at 16:28














2












2








2







One way is to create a string in MySQL Date format (YYYY-MM-DD) using string functions such as Concat() and Lpad():



SELECT *
FROM your_table
WHERE CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0'))
BETWEEN '2018-11-09' AND '2019-03-05'




Based on further discussion in comments, if you can input the year, month, and day value separately for the given data range(s); instead of creating a date using functions, we can directly use the respective columns instead. This will also allow us to utilize indexes (if defined) on these columns.



SELECT *
FROM your_table
WHERE
/* Condition for the month in the start date of the range */
(YEAR = 2018 AND MONTH = 11 AND DAY >= 9)
OR
/* Condition for the rest of the months in start date year */
(YEAR = 2018 AND MONTH > 11)
OR
/* Condition for the month in the end date of the range */
(YEAR = 2019 AND MONTH = 3 AND DAY <= 5)
OR
/* Condition for the rest of the months in end date year */
(YEAR = 2019 AND MONTH < 3)
OR
/* Condition for the years between the start and end date */
(YEAR > 2018 AND YEAR < 2019)


Above mentioned conditions can be compressed further. But I have written in this manner, for ease of understand-ability.





However, it is recommended to create another column to store the date in Date format. If you cannot make changes to the application code, and if your MySQL version >= 5.7, you can look at Generated Columns, and refer to that in your SELECT query instead.



ALTER TABLE your_table
ADD COLUMN date_col DATE
GENERATED ALWAYS AS CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0')) STORED;


Then, the SELECT query becomes trivial:



SELECT * FROM your_table
WHERE date_col BETWEEN '2018-11-09' AND '2019-03-05'





share|improve this answer















One way is to create a string in MySQL Date format (YYYY-MM-DD) using string functions such as Concat() and Lpad():



SELECT *
FROM your_table
WHERE CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0'))
BETWEEN '2018-11-09' AND '2019-03-05'




Based on further discussion in comments, if you can input the year, month, and day value separately for the given data range(s); instead of creating a date using functions, we can directly use the respective columns instead. This will also allow us to utilize indexes (if defined) on these columns.



SELECT *
FROM your_table
WHERE
/* Condition for the month in the start date of the range */
(YEAR = 2018 AND MONTH = 11 AND DAY >= 9)
OR
/* Condition for the rest of the months in start date year */
(YEAR = 2018 AND MONTH > 11)
OR
/* Condition for the month in the end date of the range */
(YEAR = 2019 AND MONTH = 3 AND DAY <= 5)
OR
/* Condition for the rest of the months in end date year */
(YEAR = 2019 AND MONTH < 3)
OR
/* Condition for the years between the start and end date */
(YEAR > 2018 AND YEAR < 2019)


Above mentioned conditions can be compressed further. But I have written in this manner, for ease of understand-ability.





However, it is recommended to create another column to store the date in Date format. If you cannot make changes to the application code, and if your MySQL version >= 5.7, you can look at Generated Columns, and refer to that in your SELECT query instead.



ALTER TABLE your_table
ADD COLUMN date_col DATE
GENERATED ALWAYS AS CONCAT(`YEAR`, '-', LPAD(`MONTH`,2,'0'), '-', LPAD(`DAY`,2,'0')) STORED;


Then, the SELECT query becomes trivial:



SELECT * FROM your_table
WHERE date_col BETWEEN '2018-11-09' AND '2019-03-05'






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 21:28

























answered Nov 24 '18 at 9:11









Madhur BhaiyaMadhur Bhaiya

19.6k62236




19.6k62236













  • Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:24











  • @NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

    – Madhur Bhaiya
    Nov 24 '18 at 20:34











  • Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

    – Nurzhan Nogerbek
    Nov 24 '18 at 21:04











  • @NurzhanNogerbek check the updated answer now.

    – Madhur Bhaiya
    Nov 24 '18 at 21:29











  • @NurzhanNogerbek any updates ? Does it work or not ?

    – Madhur Bhaiya
    Nov 25 '18 at 16:28



















  • Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

    – Nurzhan Nogerbek
    Nov 24 '18 at 20:24











  • @NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

    – Madhur Bhaiya
    Nov 24 '18 at 20:34











  • Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

    – Nurzhan Nogerbek
    Nov 24 '18 at 21:04











  • @NurzhanNogerbek check the updated answer now.

    – Madhur Bhaiya
    Nov 24 '18 at 21:29











  • @NurzhanNogerbek any updates ? Does it work or not ?

    – Madhur Bhaiya
    Nov 25 '18 at 16:28

















Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

– Nurzhan Nogerbek
Nov 24 '18 at 20:24





Thank you for your answer! Unfortunatly I can't change the structure of the table. Your code return result but the problem is speed. It's processed too long. In fact in my real table many records with the same day, month, year. The difference only in values column. That 3 colomn as I said has partitioning. Is it possible to get data for each day separately and then combine them all. Do you have any ideas about that?

– Nurzhan Nogerbek
Nov 24 '18 at 20:24













@NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

– Madhur Bhaiya
Nov 24 '18 at 20:34





@NurzhanNogerbek share the indexing details on your table. From your input date, eg, 2018-11-09, can you extract the day, month and year out and provide that as an input to the query instead ?

– Madhur Bhaiya
Nov 24 '18 at 20:34













Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

– Nurzhan Nogerbek
Nov 24 '18 at 21:04





Yes, I can. As input I can send year, month, day values separately instead of 2018-11-09 if it could solve my problem. You have ideas?

– Nurzhan Nogerbek
Nov 24 '18 at 21:04













@NurzhanNogerbek check the updated answer now.

– Madhur Bhaiya
Nov 24 '18 at 21:29





@NurzhanNogerbek check the updated answer now.

– Madhur Bhaiya
Nov 24 '18 at 21:29













@NurzhanNogerbek any updates ? Does it work or not ?

– Madhur Bhaiya
Nov 25 '18 at 16:28





@NurzhanNogerbek any updates ? Does it work or not ?

– Madhur Bhaiya
Nov 25 '18 at 16:28


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456520%2fhow-select-all-values-between-2-date-by-year-month-day-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)