Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Date from two cols in mySql

Tags:

date

sql

mysql

I have a table in my database with two columns: month (1-12) and year (yyyy).

I need to select records between two dates, for exemple

select * from Calendar a where SOMEDATE between STARTDATE and ENDDATE.

So the question is: how can I create the STARTDATE and the ENDDATE from this two columns I have?

like image 511
Bernat Avatar asked Mar 07 '26 17:03

Bernat


1 Answers

...where SOMEDATE between 
STR_TO_DATE(CONCAT_WS('-',STARTYEAR,STARTMONTH,1),'%Y-%m-%d')
and
DATE_SUB(
    STR_TO_DATE(CONCAT_WS('-',ENDYEAR,ENDMONTH + 1,1),'%Y-%m-%d')
    , INTERVAL DAY 1
)

Note that we convert both parts to type date, and use date_sub to subtract a single day from ENDMONTH + 1, since we don't know how many days there are in the relevant month.

like image 163
davek Avatar answered Mar 10 '26 14:03

davek