Skip to content

Instantly share code, notes, and snippets.

@SuN-80
SuN-80 / utf8-fix-encoding.sql
Last active February 20, 2018 14:11
MySQL Fix for common UTF-8 Character Encoding Problems
-- based on UTF-8 Encoding Debugging Chart: http://www.i18nqa.com/debug/utf8-debug.html
UPDATE table SET field = replace(field, '€', '€');
UPDATE table SET field = replace(field, '‚', '‚');
UPDATE table SET field = replace(field, 'Æ’', 'ƒ');
UPDATE table SET field = replace(field, '„', '„');
UPDATE table SET field = replace(field, '…', '…');
UPDATE table SET field = replace(field, '†', '†');
UPDATE table SET field = replace(field, '‡', '‡');
UPDATE table SET field = replace(field, 'ˆ', 'ˆ');
UPDATE table SET field = replace(field, '‰', '‰');
@SuN-80
SuN-80 / fix-utf8.sql
Created February 20, 2018 12:53
Fix double-encoded UTF8 characters
-- In an utf-8 table
UPDATE mytable SET
field = CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8);
@SuN-80
SuN-80 / search-replace.sql
Created February 20, 2018 12:43
Search/Replace All occurrences in MySQL
UPDATE mytable SET field = replace(field, 'search_for', 'replace_with');
@SuN-80
SuN-80 / missing-primary-keys.sql
Created February 20, 2018 12:41
Find missing/deleted Primary keys in a table
SELECT mytable.id +1
FROM mytable
WHERE ( mytable.id +1 )
NOT IN (
SELECT mytable.id
FROM mytable
)
ORDER BY id
LIMIT 1;
@SuN-80
SuN-80 / reverse-names.sql
Created February 20, 2018 12:37
Flip First and Last Names in MySQL
-- "Lastname, Firstname" to "Firstname Lastname"
UPDATE mytable set field = CONCAT(
TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)),
' ',
TRIM(SUBSTRING_INDEX(field,',',1))
);
@SuN-80
SuN-80 / find-delete-duplicates.sql
Last active December 9, 2019 09:54
Find and delete duplicates in MySQL
-- Find Duplicates and group by duplicated value
SELECT field, COUNT(*)
FROM mytable GROUP BY field
HAVING count(*) > 1
-- Find Duplicates and list each row
SELECT a.field, a.target_field
FROM mytable a
INNER JOIN (
SELECT target_field
@SuN-80
SuN-80 / devbridge-autocomplete-fulltext-fix.js
Created March 8, 2017 14:34
Keywords highlight fix for FULLTEXT indexes (devbridge Autocomplete)
formatResult: function (suggestion, currentValue) {
var keywords = currentValue.split(" ");
var suggest = suggestion.value;
$(keywords).each(function() {
suggest = suggest.replace(new RegExp('(' + this + ')(?![^<]*>|[^<>]*</)', 'gi'), '<strong>' + this + '</strong>');
});
return suggest;
}