That doesn't look like output from your commands - is it some debug output instead?
What you have looks correct, in that it could be fed into the shell and give what you asked for. Note that 'aaa'\''bbb'
is identical to "aaa'bbb"
!
Side note: your variables look a bit overcomplicated, and you repeat $(date +%Y-%m-%d)
; the following may help:
lastmonth=$(date -d "-1 month" +%Y-%m-01) thismonth=$(date +%Y-%m-01) today=$(date +%Y-%m-%d)
and the SQL query may be more idiomatic as ... where date between '$lastmonth' and '$thismonth' ...
.