diff --git a/lib/authlib.php b/lib/authlib.php
index 0ea720d7a3..4ad42a6c65 100644
--- a/lib/authlib.php
+++ b/lib/authlib.php
@@ -1028,7 +1028,7 @@ function signup_validate_data($data, $files) {
 
     } else if (empty($CFG->allowaccountssameemail)) {
         // Make a case-insensitive query for the given email address.
-        $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid';
+        $select = $DB->sql_equal('email', ':email', false, false) . ' AND mnethostid = :mnethostid';
         $params = array(
             'email' => $data['email'],
             'mnethostid' => $CFG->mnet_localhost_id,
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index d10f31d7a9..52e1904a6f 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -4400,7 +4400,7 @@ function authenticate_user_login($username, $password, $ignorelockout=false, &$f
 
     } else if (!empty($CFG->authloginviaemail)) {
         if ($email = clean_param($username, PARAM_EMAIL)) {
-            $select = "mnethostid = :mnethostid AND LOWER(email) = LOWER(:email) AND deleted = 0";
+	    $select = $DB->sql_equal('email', ':email', false,false) . ' AND mnethostid = :mnethostid AND deleted=0';
             $params = array('mnethostid' => $CFG->mnet_localhost_id, 'email' => $email);
             $users = $DB->get_records_select('user', $select, $params, 'id', 'id', 0, 2);
             if (count($users) === 1) {
diff --git a/login/lib.php b/login/lib.php
index b391ea7b71..51a3e35219 100644
--- a/login/lib.php
+++ b/login/lib.php
@@ -97,7 +97,7 @@ function core_login_process_password_reset($username, $email) {
         // 1/ the email is not guaranteed to be unique - TODO: send email with all usernames to select the account for pw reset
         // 2/ mailbox may be case sensitive, the email domain is case insensitive - let's pretend it is all case-insensitive.
 
-        $select = $DB->sql_like('email', ':email', false, true, false, '|') .
+        $select = $DB->sql_like('email', ':email', false, false, false, '|') .
                 " AND mnethostid = :mnethostid AND deleted=0 AND suspended=0";
         $params = array('email' => $DB->sql_like_escape($email, '|'), 'mnethostid' => $CFG->mnet_localhost_id);
         $user = $DB->get_record_select('user', $select, $params, '*', IGNORE_MULTIPLE);
diff --git a/user/edit_form.php b/user/edit_form.php
index 595c733389..f0f4f1ce1d 100644
--- a/user/edit_form.php
+++ b/user/edit_form.php
@@ -212,7 +212,7 @@ class user_edit_form extends moodleform {
             $errors['email'] = get_string('invalidemail');
         } else if (($usernew->email !== $user->email) && empty($CFG->allowaccountssameemail)) {
             // Make a case-insensitive query for the given email address.
-            $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+            $select = $DB->sql_equal('email', ':email', false,false) . ' AND mnethostid = :mnethostid AND id <> :userid';
             $params = array(
                 'email' => $usernew->email,
                 'mnethostid' => $CFG->mnet_localhost_id,
diff --git a/user/editadvanced_form.php b/user/editadvanced_form.php
index 2622dc8917..1c5453a666 100644
--- a/user/editadvanced_form.php
+++ b/user/editadvanced_form.php
@@ -300,7 +300,7 @@ class user_editadvanced_form extends moodleform {
                 $err['email'] = get_string('invalidemail');
             } else if (empty($CFG->allowaccountssameemail)) {
                 // Make a case-insensitive query for the given email address.
-                $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+                $select = $DB->sql_equal('email', ':email', false, false) . ' AND mnethostid = :mnethostid AND id <> :userid';
                 $params = array(
                     'email' => $usernew->email,
                     'mnethostid' => $CFG->mnet_localhost_id,
diff --git a/user/emailupdate.php b/user/emailupdate.php
index 4080eada0f..328d0c1edc 100644
--- a/user/emailupdate.php
+++ b/user/emailupdate.php
@@ -62,7 +62,7 @@ if (empty($preferences['newemailattemptsleft'])) {
     // Detect duplicate before saving.
     if (empty($CFG->allowaccountssameemail)) {
         // Make a case-insensitive query for the given email address.
-        $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+        $select = $DB->sql_equal('email', ':email', false,false) . ' AND mnethostid = :mnethostid AND id <> :userid';
         $params = array(
             'email' => $user->email,
             'mnethostid' => $CFG->mnet_localhost_id,
diff --git a/user/externallib.php b/user/externallib.php
index 0d7daad5fa..a27d153967 100644
--- a/user/externallib.php
+++ b/user/externallib.php
@@ -207,7 +207,7 @@ class core_user_external extends external_api {
                 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
             } else if (empty($CFG->allowaccountssameemail)) {
                 // Make a case-insensitive query for the given email address.
-                $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid';
+                $select = $DB->sql_equal('email', ':email', false,false) . ' AND mnethostid = :mnethostid';
                 $params = array(
                     'email' => $user['email'],
                     'mnethostid' => $user['mnethostid']
