So I have a users table where the user.username has many duplicates like:
username
and Username
and useRnAme
john
and John
and jOhn
That was a bug and these three records should have been only one.
I'm trying to come up with a SQL query that lists all of these cases ordered by their creation date, so ideally the result should be something like this:
username jan01
useRnAme jan02
Username jan03
john feb01
John feb02
jOhn feb03
Any suggestions will be much appreciated
Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.
Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.
SQL Server is, by default case insensitive; however, it is possible to create a case sensitive SQL Server database and even to make specific table columns case sensitive.
SQL Case insensitivity is to use the query statements and the keywords tables and columns by specifying them in capital or small letters of alphabets. SQL keywords are by default set to case insensitive that means that the keywords are allowed to be used in lower or upper case.
Leaving aside the issue of case sensitivity for a moment, the basic strategy is:
SELECT username, create_date FROM your_table
WHERE username IN
(SELECT username FROM your_table GROUP BY username HAVING COUNT(*) > 1)
ORDER BY username, create_date
Many RDBMSes (including MySQL assuming that you are using CHAR or VARCHAR for the username column), perform case-insensitive searching by default. For those databases, the above solution will work. To solve the case sensitivity issue for other products , wrap all except the first occurrence of username in the uppercase conversion function specific to your RDBMS:
SELECT username, create_date FROM your_table
WHERE UPPER(username) IN
(SELECT UPPER(username) FROM your_table GROUP BY UPPER(username) HAVING COUNT(*) > 1)
ORDER BY username, create_date
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With