How to filter results by weekday / weekdays?


I want to filter results by weekday, meaning to ger trading results made on Tuesday or any other day. I also want to get results of trades made on wednesdays and thursdays ( a few selected days together.

(I get the days / day selection as an array from a form)

Here is the query I coded according to explanations I found On the internet:

$sql = "SELECT ticket, o_time, type, size, item, o_price, s_l, t_p, c_time, c_price, profit	WHERE DATEPART(day, c_time)[day] = 2
			FROM data ORDER BY c_time";

Here is the error message:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\forex_analyzer\forex_all.php on line 413

I believe that I got the query wrong

I appreciate your help,

  1. SELECT queries are alway SELECT ... FROM ... WHERE, not SELECT ... WHERE ... FROM
  2. MySQL doesn’t have a function called DATEPART, neither does Postgres. If you use MySQL, you probably want dayofweek or if you use Postgres you probably meant date_part?
  3. Neither MySQL’s DAYOFWEEK, nor Postgres’ date_part return a string, they both return a numeric value, so you should directly, instead of trying to access some array key which isn’t there.

worry not, SQL Server does (the square brackets on the column alias are a big clue)

and it returns an INT, so WHERE DATEPART(day, c_time)[day] = 2 is fine

but you nailed the problem with the placement of the WHERE clause

1 Like

THanks, one problem solved.

$sql = "SELECT ticket, o_time, type, size, item, o_price, s_l, t_p, c_time, c_price, profit
			FROM data WHERE dayofweek(c_time) = 3 ORDER BY c_time";

Yet how do I get a query where I need 2 days or 3 days ?

e.g. where dayofweek(c_time) = 3 and dayofweek(c_time) = 5 together
dayofweek(c_time) = 2 anddayofweek(c_time) = 4 and dayofweek(c_time) = 5

and the user can select any combination of days , any number of days

WHAT??!! you switched from SQL Server to MySQL just like that???

man, if only it were that easy #smh

with an IN list

WHERE DAYOFWEEK(c_time) IN ( 2, 3, 5 )
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.