Substr inside a query?!?!

I’m trying to figure out how to do this:

FROM ".$prefix."_score_info WHERE ".substr("regdate", 1, 4)." = $currSeason

The currSeason is 2010 and the “regdate” field looks like this: XXXX-XX-XX being Year-Month-Day…

Can this be done in someway?

Thanks in advance :slight_smile:

WHERE year(regdate) = $currSeason
but you have to learn not to mix SQL and PHP and then learn some of Mysql functions, string (which you were asking) or date (which you actually need)

If this were in a string, then as you are starting from the beginning you could just use a wildcard:

"SELECT columns FROM {$prefix}_score_info WHERE regdate LIKE '{$currSeason}%'"
WHERE regdate >= '2010-01-01'
  AND regdate  < '2011-01-01'

this is the only way to avoid a table scan

That’s a very good point (unsurprisingly :p) - it would be lighter on the database engine.

In terms of your PHP, that would be:

$nextSeason = $currSeason + 1;
$Query = "SELECT columns FROM {$prefix}_score_info WHERE regdate >= '{$currSeason}-01-01' AND regdate < '{$nextSeason}-01-01'";