/    Sign up×
Community /Pin to ProfileBookmark

month/year with concat/date_format in mysql query

I am working with a DB that I cannot alter. I was wondering if there was a way to achieve what I am attempting in a mysql_query or if I have to format through PHP. Long story short, I have to pull be two fields from the DB, entry month and entry year. I need to pull all instances, for example, between July last year and June this year. I was attempting to do this with concat_ws and date_format but it is not working correctly. This is my current query.

[code=php]$select = mysql_query(“SELECT *, CONCAT_WS(‘-‘,entry_month,entry_year) AS condate FROM kpi
WHERE DATE_FORMAT(condate,’%m-%Y’) >= ‘$start_time’ AND
DATE_FORMAT(condate,’%m-%Y’) <= ‘$end_time'”);[/code]

This however is not working correctly because it is not finding condate as a useable variable. Is this possible in the query itself?

I’ve also tried this but it isn’t working as no results are returned.

[code=php]$select = mysql_query(“SELECT * FROM kpi
WHERE DATE_FORMAT(CONCAT_WS(‘-‘,entry_month,entry_year),’%m-%Y’) >= ‘$start_time’ AND
DATE_FORMAT(CONCAT_WS(‘-‘,entry_month,entry_year),’%m-%Y’) <= ‘$end_time'”);[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — Instead of this:
[code=php]
CONCAT_WS('-',entry_month,entry_year) AS condate[/code]

I think you can do this:
[code=php]
(entry_month & '-' & entry_year) AS condate[/code]
Copy linkTweet thisAlerts:
@ephmynusauthorJul 02.2007 — Thanks, but condate, as it was set before, is working properly. However, it won't allow it in the same query because it is stating it does not recognize it as a field. Once I fetch the array, condate works fine as I had it above.
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — ...delete...
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — I've also tried this but it isn't working as no results are returned.

[code=php]$select = mysql_query("SELECT * FROM kpi
WHERE DATE_FORMAT(CONCAT_WS('-',entry_month,entry_year),'%m-%Y') >= '$start_time' AND
DATE_FORMAT(CONCAT_WS('-',entry_month,entry_year),'%m-%Y') <= '$end_time'");[/code]
[/QUOTE]

In the above, perhaps you want STR_TO_DATE() instead of DATE_FORMAT().
×

Success!

Help @ephmynus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...