Editing
Data cleaning
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Check if field value was not fulfilled == === By purpose === <table border="1" style="width: 100%"> <tr style="background-color: #555555; color: #ffffff;"> <td style="background-color: #777; color: #ffffff;">Purpose</td> <td style="background-color: #777; color: #ffffff; width: 350px;">Method (MySQL query syntax)</td> <td style="vertical-align: top;">Value1: <br />Fulfilled value what I want</td> <td style="vertical-align: top;">Value2: <br />Fulfilled value NOT I want</td> <td style="vertical-align: top;">Value3: <br />0</td> <td style="vertical-align: top;">Value4: <br />NULL value</td> <td style="vertical-align: top;">Value5: <br />Empty or white-spaces characters</td> </tr> <tr> <td>values were not fulfilled or empty <br />(not contains 0)</td> <td>{{kbd | key=WHERE column_name IS NULL <br /> OR LENGTH(TRIM( column_name )) = 0}}</td> <td></td> <td></td> <td></td> <td>V</td> <td>V</td> </tr> <tr> <td>values were not fulfilled or empty<br />(contains 0)</td> <td></td> <td></td> <td></td> <td>V</td> <td>V</td> <td>V</td> </tr> <tr> <td>values were fulfilled and non-empty<br />(not contains 0)</td> <td></td> <td>V</td> <td>V</td> <td></td> <td></td> <td></td> </tr> <tr> <td>values were fulfilled and non-empty<br />(contains 0)</td> <td>{{kbd | key=<nowiki>WHERE LENGTH(TRIM( column_name )) > 0</nowiki>}}</td> <td>V</td> <td>V</td> <td>V</td> <td></td> <td></td> </tr> <tr> <td>values (1) were not fulfilled or empty values (2) NOT I want <br />(not contains 0)</td> <td>{{kbd | key=WHERE column_name IS NULL <br /> OR LENGTH(TRIM( column_name )) = 0 <br /> OR column_name LIKE 'values NOT I want'}}</td> <td></td> <td>V</td> <td></td> <td>V</td> <td>V</td> </tr> <tr> <td>values (1) were not fulfilled or empty values (2) NOT I want <br />(contains 0)</td> <td></td> <td></td> <td>V</td> <td>V</td> <td>V</td> <td>V</td> </tr> </table> === By datatype === ==== VARCHAR and NOT allows NULL value ==== Using NULLIF() function<ref>[https://www.w3schools.com/sql/func_mysql_nullif.asp MySQL NULLIF() Function]</ref> SQL query: <pre> SELECT NULLIF(TRIM(`my_column`), "") </pre> Example result: <pre> SELECT NULLIF(null, ""); -- return NULL SELECT NULLIF("", ""); -- return NULL SELECT NULLIF(TRIM(" "), ""); -- return NULL SELECT NULLIF(TRIM("not empty string "), ""); -- return "not empty string" </pre> ==== VARCHAR and allows NULL value ==== <table border="1" style="width: 100%"> <tr style="background-color: #555555; color: #ffffff;"> <td>data type of column</td> <td>possible column values</td> <td>method1: <br />find not fulfilled or empty values</td> <td>method2: <br />find fulfilled and non-empty values</td> <td>method3: <br />find NULL values</td> <td>method4: <br />find not NULL values</td> </tr> <tr> <td rowspan="5">{{kbd | key = VARCHAR}} and allows {{kbd | key = NULL}}</td> <td>fulfilled value ex:123</td> <td></td> <td>V</td> <td></td> <td>V</td> </tr> <tr> <td>{{kbd | key = NULL}} type:null</td> <td>V</td> <td></td> <td>V</td> <td></td> </tr> <tr> <td>{{kbd | key = 'NULL'}} type:string</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>0</td> <td></td> <td>V</td> <td></td> <td>V</td> </tr> <tr> <td>EMPTY ex: <nowiki>'' or space(s) ' '</nowiki></td> <td>V</td> <td></td> <td></td> <td>V</td> </tr> </table> symbol V: means the column value will be able to find by means of the method * method1: ** {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE COALESCE(column_name, '') = ''</nowiki>}}<ref>[http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php MySQL COALESCE() function - w3resource]</ref> ** {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE column_name IS NULL OR LENGTH(TRIM( column_name )) = 0</nowiki>}} **{{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE column_name IS NULL OR column_name = ''</nowiki>}}<ref>[http://stackoverflow.com/questions/17832906/how-to-check-if-field-is-null-or-empty-mysql How to check if field is null or empty mysql? - Stack Overflow]</ref> * method2: ** {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE column_name > ''</nowiki>}} ** {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE LENGTH(TRIM( column_name )) > 0</nowiki>}} ** {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE LENGTH(TRIM( column_name )) != 0</nowiki>}} * method3: {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE column_name IS NULL</nowiki>}} * method4: {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE column_name IS NOT NULL</nowiki>}} ==== VARCHAR or numeric ==== <table border="1" style="width: 100%"> <tr style="background-color: #555555; color: #ffffff;"> <td>data type of column</td> <td>possible column values</td> <td>method5: <br />find values within the range</td> <td>method6: <br />find values out of the range, empty & NULL values</td> </tr> <tr> <td rowspan="4">{{kbd | key = VARCHAR}} or numeric</td> <td>values within the range ex: min ≤ value ≤ max</td> <td>V</td> <td></td> </tr> <tr> <td>values out of range</td> <td></td> <td>V</td> </tr> <tr> <td>{{kbd | key = NULL}}</td> <td></td> <td>V</td> </tr> <tr> <td>EMPTY ex: <nowiki>'' or space ' '</nowiki></td> <td></td> <td>V</td> </tr> </table> * method5: {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE column_name BETWEEN min AND max</nowiki>}} * method6: {{kbd | key = <nowiki>SELECT * FROM `my_table` WHERE ( (COALESCE(column_name, '') = '') OR (column_name NOT BETWEEN min AND max) )</nowiki>}} ==== datetime and allows NULL value ==== possible column values # {{kbd | key = {{Template:Today}}}} # {{kbd | key = 00-00-00 00:00:00}} # {{kbd | key = NULL}} === is null === Fill 0 if the value is NA or NULL * MySQL SQL syntax: COALESCE(): {{kbd | key = SELECT COALESCE(column_name, 0) }} or {{kbd | key = SELECT COALESCE(column_name, 'other_filled_value') }} ** (1) Using [http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php COALESCE() function] to replace the NULL value with 0. ** (2) The case: 0/0 = null should be handled. * MySQL SQL syntax: combined IF() & ISNULL():{{kbd | key = SELECT IF(ISNULL((column_name), 0, column_name) }} or {{kbd | key = SELECT IF(ISNULL((column_name), 'other_filled_value', column_name) }} * python: [http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.fillna.html pandas.DataFrame.fillna — pandas 0.16.0 documentation] "Fill NA/NaN values using the specified method" Find whether a variable is NULL. [http://ideone.com/tKv2gQ online demo] * PHP [http://tw2.php.net/is_null is_null] to find type:null null NOT type:string <nowiki>'null'</nowiki> {{exclaim}} * Google spreadsheet / Excel: ** [https://support.google.com/drive/answer/3093348 ISERR(value)] " value - The value to be verified as an error type other than #N/A." ex: {{kbd | key = #NULL!}} ** If the cell value is exactly {{kbd | key = NULL}} not {{kbd | key = #NULL!}}, You may use {{kbd | key = <nowiki>COUNTIF(value, "NULL")</nowiki>}} or {{kbd | key = <nowiki>EXACT(value, "NULL")</nowiki>}} * MySQL SQL syntax: {{kbd | key = SELECT * FROM table WHERE column IS NULL;}}<ref>[http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html MySQL :: MySQL 5.0 Reference Manual :: 3.3.4.6 Working with NULL Values]</ref> * R: is.null(): [https://stat.ethz.ch/R-manual/R-devel/library/base/html/NULL.html R: The Null Object] * Excel<ref>[https://errerrors.blogspot.com/2018/09/check-if-cell-value-is-null-in-excel.html 如何判斷 Excel 儲存格的欄位值是 NULL]</ref> Find whether a variable is NOT NULL * MySQL SQL syntax: {{kbd | key = SELECT * FROM table WHERE column IS NOT NULL;}} Find whether a variable is NOT {{kbd | key = #N/A}} * Excel: {{kbd | key = =NOT(ISERROR(cell_value))}} javascript * [http://www.w3schools.com/jsref/jsref_isnan.asp JavaScript isNaN() Function] === check if field value was not fulfilled: NULL, empty value === {{exclaim}} NOT include those data which its field value fulfilled with default value automatically ([http://sqlfiddle.com/#!2/9b01e/3/0 demo on sqlfiddle]) # {{Gd}} quick solution: find records with {{kbd | key = NULL}} value OR empty, space value #* MySQL solution: {{kbd | key = SELECT * FROM table_name WHERE column_name IS NULL OR LENGTH(TRIM( column_name )) = 0;}} # find records with {{kbd | key = NULL}} value: (note: not {{kbd | key = #NULL!}}) #* MySQL solution: {{kbd | key = SELECT * FROM table_name WHERE column_name IS NULL;}} #* EXCEL: {{kbd | key =<nowiki>=EXACT(A2, "NULL")</nowiki>}} # find records with empty value: (not contains {{kbd | key = NULL}} value) #* MySQL: {{kbd | key = SELECT * FROM table_name WHERE LENGTH(TRIM( column_name )) = 0;}} {{Exclaim}} SQL query {{kbd | key =SELECT * FROM table_name WHERE column_name IS NOT NULL}} includes empty value #* MS SQL Server: {{kbd | key = SELECT * FROM table_name WHERE LEN( LTRIM(RTRIM(column_name)) ) = 0;}} <ref>[http://www.1keydata.com/tw/sql/sql-trim.html SQL TRIM 函數 - 1Keydata SQL 語法教學]</ref> # Excel starting date: 1900/1/0 (converted time formatted value from 0), 1900/1/1 (converted time formatted value from 1), 1900/1/2 ... #* solution: step1: Replace the year > 100 from this year with empty value at EXCEL: {{kbd | key =<nowiki>=IF(ISERR(YEAR(A2)), "", IF(YEAR(A2)<1914, "", A2))</nowiki>}} (this formula also handle empty value and non well-formatted column value ex: 0000-12-31 ) ; step2: change the format of cell to time format #* trivial approach : EXCEL: {{kbd | key =<nowiki>=IF(ISERR(YEAR(A2)), "", IF(YEAR(A2)-YEAR(NOW())>100, "", A2))</nowiki>}} {{exclaim}} this formula could not handle empty value because it return 0. If I change the format of cell to time format, 0 will become 1900/1/0. # Using PHP #* [http://php.net/manual/en/function.empty.php empty()] function to find 0, null, false, empty string, empty array values. #* <code>if(empty($var) && $var !== 0 && $var !== "0"){ .. } </code> to find null, false, empty string, empty array values BUT not 0. # check if field value was NULL & not equal to some value #* MySQL: [http://stackoverflow.com/questions/17097890/mysql-select-on-fields-containing-null-values MySql select on fields containing null values - Stack Overflow] === check if field value was fulfilled === length of string > 0 * MySQL: {{kbd | key = SELECT * FROM table_name WHERE LENGTH(TRIM( column_name )) != 0;}} [http://sqlfiddle.com/#!9/e0e5f/1/0 demo][http://ideone.com/rt6D2A] column value is not {{kbd | key = null}} or {{kbd | key = 0}} * Excel: {{kbd | key = COUNTIFS(criteria_range1, "<>NULL", criteria_range1, "<>0")}}<ref>[https://www.ablebits.com/office-addins-blog/2014/07/10/excel-countifs-multiple-criteria/ Excel COUNTIFS and COUNTIF with multiple criteria – examples of usage]</ref> find if number or cell value is positive integer * EXCEL: {{kbd | key =<nowiki>=IFERROR(IF(AND(INT( value )= value, value>0), TRUE, FALSE), FALSE)</nowiki>}}<ref>[http://www.ozgrid.com/forum/showthread.php?t=24464 Check if number is an Integer]</ref> [https://docs.google.com/spreadsheets/d/1JlY9RsFDO-I9BuFa_Oegnrq5-V5vJwt4jVYUZBlyFLM/edit?usp=sharing online demo] check numeric range * MySQL: {{kbd | key = SELECT * FROM table_name WHERE column_name BETWEEN ''min_number'' AND ''max_number'';}} the value >= ''min_number'' AND value <= ''max_number'' ( ''min_number'' ≤ value ≤ ''max_number'' ) find NOT empty records means records without NULL or empty value: * MySQL: {{kbd | key =<nowiki>SELECT * FROM table_name WHERE LENGTH(TRIM( column_name )) != 0;</nowiki>}} * MySQL: {{kbd | key =<nowiki>SELECT * FROM table_name WHERE column_name != '' AND column_name IS NOT NULL;</nowiki>}}
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information