Question: I have this vb.NET code and would like to write a function in MySQL that does the same thing.

```
Public Function IsWeekdayTimeInRange(DateToCheck As DateTime, StartDayOfWeek As DayOfWeek,
                                                              EndDayOfWeek As DayOfWeek) As Boolean

    Dim WeekdaysInRange As New List(Of DayOfWeek)
    WeekdaysInRange.Add(StartDayOfWeek)
    Dim i As Integer = StartDayOfWeek
    While i <> EndDayOfWeek
        i += 1
        i = If(i = 7, 0, i)
        WeekdaysInRange.Add(i)
    End While

    Return WeekdaysInRange.Contains(DateToCheck.DayOfWeek)
End Function

```

In MySQL I'm currently at this point but since I've never written a SQL function I need some help with this.

```
DELIMITER |
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
    DECLARE weekdayToCheck INT;
    DECLARE result BOOLEAN;
    DECLARE i INT;
    SET result = false;
    SET weekdayToCheck = WEEKDAY(d);
    SET i = s;
    WHILE(i <> e) DO

        /* more logic here */

    END WHILE;

    RETURN result;
END; |

DELIMITER ;

```

Edit: Since I now learnt that MySQL has `Weekday(Monday) = 0` my functions also require conversion to `Weekday(Sunday) = 0` because my data use that format.

Answer: 
Not sure what your definition of "between" is but here I used larger than and smaller than

```
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
    DECLARE weekdayToCheck INT;
    DECLARE result INT;
    SET weekdayToCheck = WEEKDAY(d);
    SET result = 0;

    IF (s < e) THEN 
       IF  (weekdayToCheck > s AND weekdayToCheck < e) THEN SET result = 1;
       END IF;
    END IF;

    IF (s > e) THEN
       IF (weekdayToCheck > s OR weekdayToCheck < e) THEN SET result = 1;
       END IF;        
    END IF;

    RETURN result; 
END;

```

Perhaps all the if/else clause can be shortened somewhat.
Considering a week starts from Monday. Hope this helps:

```
DELIMITER |
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
  DECLARE weekdayToCheck INT;
  DECLARE result TINYINT(1);
  SET weekdayToCheck =  WEEKDAY(d);
  SET result = 0;
  IF(weekdayToCheck > s AND weekdayToCheck < e) THEN
    SET result = 1;
  ELSEIF (s > e AND weekdayToCheck < s AND weekdayToCheck < e) THEN
    SET result = 1;
  END IF;

RETURN result;
END; |

DELIMITER ;

```