Canonical Cheat Sheet
หน้านี้คือ reference เดียวที่คุณต้องการ — เปิดไว้ข้างๆ ตอนทำงานจริง
Data Type Comparison Table
Section titled “Data Type Comparison Table”| Data Type | Google Sheets | C | Python | SQL |
|---|---|---|---|---|
| Single char | ไม่มีแยก — ทุกอย่างคือ string | char (1 byte) | str (length 1) | CHAR(1) |
| Variable text | String (auto) | char[] / char* | str | VARCHAR(n) |
| Integer | Number (auto) | int (4B), long (8B) | int (arbitrary size) | INTEGER, BIGINT |
| Float | Number (auto, 64-bit) | float (4B), double (8B) | float (64-bit) | REAL, DOUBLE PRECISION |
| Exact decimal | ไม่มี (ใช้ Number + format) | ไม่มี (ใช้ integer cents) | Decimal | NUMERIC(p,s), DECIMAL(p,s) |
| Boolean | TRUE / FALSE | bool (stdbool.h), 0/1 | True / False | BOOLEAN |
| Date | Date (serial number) | ไม่มี (ใช้ struct/string) | datetime.date | DATE |
| Datetime | Date+Time (serial number) | time_t, struct tm | datetime.datetime | TIMESTAMP |
| Missing value | Blank cell | NULL pointer / sentinel | None, pd.NA, np.nan | NULL |
| Empty string | ="" (ดูว่าง แต่มี value) | "" (1 byte: null terminator) | "" (length 0) | '' (ไม่ใช่ NULL!) |
รายละเอียดแต่ละ Type
Section titled “รายละเอียดแต่ละ Type”Single Character
Section titled “Single Character”=CODE("A") → 65=CHAR(65) → "A"=LEN("A") → 1=LENB("A") → 1 // ASCII = 1 byte=LENB("ก") → 3 // Thai = 3 bytes (UTF-8)char c = 'A'; // 1 byte, stores ASCII 65printf("%d", c); // 65printf("%c", c); // Asizeof(char); // always 1ord('A') # 65chr(65) # 'A'len('A') # 1len('A'.encode()) # 1 (UTF-8 bytes)len('ก'.encode()) # 3 (UTF-8 bytes)SELECT ASCII('A'); -- 65SELECT CHR(65); -- 'A'SELECT CHAR_LENGTH('A'); -- 1SELECT OCTET_LENGTH('A'); -- 1SELECT OCTET_LENGTH('ก'); -- 3Variable Text
Section titled “Variable Text”=LEN("Hello") → 5=LEFT("Hello", 2) → "He"=CONCATENATE("A","B") → "AB"// ไม่มีขีดจำกัดความยาว (ในทางปฏิบัติ ~50,000 chars)char fixed[6] = "Hello"; // CHAR(5) — fixed size, null-terminatedchar *dynamic = "Hello"; // VARCHAR — pointer to string literalstrlen(fixed); // 5 (ไม่นับ null terminator)sizeof(fixed); // 6 (นับ null terminator)s = "Hello"len(s) # 5s[0:2] # "He"s + " World" # "Hello World"# Python str = immutable, variable length, UTF-8 internally-- CHAR(10) = fixed, pads with spaces-- VARCHAR(100) = variable, no paddingSELECT LENGTH(CAST('HI' AS CHAR(5))); -- 5 (padded!)SELECT LENGTH(CAST('HI' AS VARCHAR(5))); -- 2 (no padding)Numbers: Integer vs Float vs Decimal
Section titled “Numbers: Integer vs Float vs Decimal”=TYPE(42) → 1 (number)=TYPE("42") → 2 (text)=TYPE(3.14) → 1 (number — ไม่แยก int/float!)
// Sheets ไม่แยก integer กับ float — ทั้งหมดคือ 64-bit float=0.1 + 0.2 → 0.3 (ดูถูก)=TEXT(0.1 + 0.2, "0.00000000000000000") → 0.300000000000000044 (จริงๆ ผิด!)int i = 42; // 4 bytes, exactlong l = 42L; // 8 bytes, exactfloat f = 3.14f; // 4 bytes, ~7 significant digitsdouble d = 3.14; // 8 bytes, ~15 significant digits
// สำหรับเงิน:int cents = 2499; // exact! (24.99 * 100)type(42) # int (arbitrary precision!)type(3.14) # float (64-bit)
from decimal import DecimalDecimal('0.1') + Decimal('0.2') # Decimal('0.3') — exact!0.1 + 0.2 # 0.30000000000000004 — inexact!-- Integer typesSELECT pg_typeof(42); -- integerSELECT pg_typeof(42::BIGINT); -- bigint
-- Float (approximate)SELECT pg_typeof(3.14::REAL); -- real (4 bytes)SELECT pg_typeof(3.14::FLOAT8); -- double precision (8 bytes)
-- Decimal (exact)SELECT pg_typeof(3.14::NUMERIC); -- numericSELECT 0.1::NUMERIC + 0.2::NUMERIC; -- 0.3 (exact!)SELECT 0.1::REAL + 0.2::REAL; -- 0.3000000X (inexact!)Boolean
Section titled “Boolean”=TRUE // boolean TRUE=FALSE // boolean FALSE=TYPE(TRUE) → 4 // type 4 = boolean
// Sheets auto-converts:=TRUE + 0 → 1=FALSE + 0 → 0=IF(1, "yes", "no") → "yes" // 1 = truthy#include <stdbool.h>bool active = true; // 1bool done = false; // 0// In C, bool is just int: 0 = false, non-zero = trueif (42) printf("truthy!"); // prints!True, False # boolbool(1) # Truebool(0) # Falsebool("") # False (empty string = falsy)bool("0") # True! (non-empty string)bool(None) # FalseSELECT TRUE, FALSE;SELECT TRUE::INTEGER; -- 1SELECT FALSE::INTEGER; -- 0-- SQL is strict: WHERE active = 1 may not work-- Use: WHERE active = TRUE or WHERE active IS TRUEDate & Datetime
Section titled “Date & Datetime”// Sheets เก็บ date เป็น serial number (integer)=DATEVALUE("2025-01-15") → 45672// Day 1 = December 30, 1899
=NOW() → serial number + fraction// fraction = เวลาเป็นสัดส่วนของ 24 ชม.// 0.5 = 12:00 PM
=TEXT(45672, "YYYY-MM-DD") → "2025-01-15"#include <time.h>time_t now = time(NULL); // seconds since 1970-01-01struct tm *t = localtime(&now);printf("%d-%02d-%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);// C has no native date type — uses epoch secondsfrom datetime import date, datetime
d = date(2025, 1, 15)dt = datetime(2025, 1, 15, 14, 30, 0)
d.isoformat() # '2025-01-15'dt.isoformat() # '2025-01-15T14:30:00'
import pandas as pdpd.Timestamp('2025-01-15')SELECT DATE '2025-01-15';SELECT TIMESTAMP '2025-01-15 14:30:00';SELECT CURRENT_DATE;SELECT CURRENT_TIMESTAMP;
-- Extract parts:SELECT EXTRACT(YEAR FROM DATE '2025-01-15'); -- 2025SELECT EXTRACT(MONTH FROM DATE '2025-01-15'); -- 1Missing Value vs Empty String
Section titled “Missing Value vs Empty String”// Blank cell (ไม่ได้พิมพ์อะไร)=ISBLANK(A1) → TRUE // truly empty=A1 = "" → TRUE // also matches ""
// Cell with ="" (typed formula)=ISBLANK(A2) → FALSE // has a value!=A2 = "" → TRUE // value is empty string
// COUNTBLANK นับทั้ง blank AND ""=COUNTBLANK(A1:A2) → 2 // !!!char *null_ptr = NULL; // no memory allocatedchar empty[] = ""; // 1 byte: just '\0'// null_ptr != empty — one is nothing, one is something emptyNone # Python's null"" # empty string (has a type!)pd.NA # pandas missing valuenp.nan # numpy missing (float!)
None == "" # Falsepd.isna(None) # Truepd.isna("") # False!SELECT NULL = ''; -- NULL (not TRUE, not FALSE!)SELECT NULL IS NULL; -- TRUESELECT '' IS NULL; -- FALSESELECT '' = ''; -- TRUE
-- COALESCE: first non-NULL valueSELECT COALESCE(NULL, '', 'default'); -- '' (empty string!)Null / Blank Decision Tree
Section titled “Null / Blank Decision Tree”สรุป Decision Tree
Section titled “สรุป Decision Tree”| สถานการณ์ | Sheets | Python / pandas | SQL |
|---|---|---|---|
| ไม่มีอะไรเลย (truly nothing) | Blank cell | None / pd.NA | NULL |
| Empty string ตั้งใจ | ="" | "" | '' |
| ตัวเลขศูนย์ | 0 | 0 | 0 |
Quick Reference: เมื่อไหร่ใช้อะไร
Section titled “Quick Reference: เมื่อไหร่ใช้อะไร”| สถานการณ์ | ใช้ type | เหตุผล |
|---|---|---|
| เก็บ ID ที่มี leading zeros | VARCHAR / string | ไม่ใช่ตัวเลข! |
| เก็บจำนวนเงิน | NUMERIC / Decimal / integer cents | ห้ามใช้ float |
| เก็บวันที่ | DATE / datetime.date | ไม่ใช่ string! |
| ค่าที่ไม่มี (missing) | NULL / None / pd.NA | ไม่ใช่ 0, ไม่ใช่ "" |
| ค่า yes/no | BOOLEAN / bool | ไม่ใช่ string “yes” |