{"id":"90c6e55ad06c9cf2","repo":"go-sql-driver/mysql","slug":"bad-value-for-field-c","errorCode":null,"errorMessage":"bad value for field: `%c`","messagePattern":"bad value for field: `%c`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"utils.go","lineNumber":121,"sourceCode":"\n/******************************************************************************\n*                           Time related utils                                *\n******************************************************************************/\n\nfunc parseDateTime(b []byte, loc *time.Location) (time.Time, error) {\n\tconst base = \"0000-00-00 00:00:00.000000\"\n\tswitch len(b) {\n\tcase 10, 19, 21, 22, 23, 24, 25, 26: // up to \"YYYY-MM-DD HH:MM:SS.MMMMMM\"\n\t\tif string(b) == base[:len(b)] {\n\t\t\treturn time.Time{}, nil\n\t\t}\n\n\t\tyear, err := parseByteYear(b)\n\t\tif err != nil {\n\t\t\treturn time.Time{}, err\n\t\t}\n\t\tif b[4] != '-' {\n\t\t\treturn time.Time{}, fmt.Errorf(\"bad value for field: `%c`\", b[4])\n\t\t}\n\n\t\tm, err := parseByte2Digits(b[5], b[6])\n\t\tif err != nil {\n\t\t\treturn time.Time{}, err\n\t\t}\n\t\tmonth := time.Month(m)\n\n\t\tif b[7] != '-' {\n\t\t\treturn time.Time{}, fmt.Errorf(\"bad value for field: `%c`\", b[7])\n\t\t}\n\n\t\tday, err := parseByte2Digits(b[8], b[9])\n\t\tif err != nil {\n\t\t\treturn time.Time{}, err\n\t\t}\n\t\tif len(b) == 10 {\n\t\t\treturn time.Date(year, month, day, 0, 0, 0, 0, loc), nil","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/utils.go#L103-L139","documentation":"Returned by parseDateTime (utils.go:121) when parsing a textual datetime and the byte at position 4 is not `-`. Position 4 is the year-month separator in the `YYYY-MM-DD ...` layout, so a wrong character means the string does not follow the expected format. parseDateTime is used when reading DATETIME/TIMESTAMP columns (with parseTime=true) and by nulltime.go.","triggerScenarios":"Reading a row where a DATETIME/TIMESTAMP-valued column contains a string whose 5th byte is not '-', e.g. a value stored as `2024/01/01 ...` (slash separator) or otherwise corrupted, while the column length is one of the accepted sizes (10,19,21-26). utils.go:120-121 fires.","commonSituations":"A non-standard datetime format inserted via loose sql_mode; a column that is declared DATETIME but holds malformed/legacy data; a character-set/encoding issue altering bytes; application code writing strings instead of proper datetime values.","solutions":["Inspect and fix the stored data so it matches `YYYY-MM-DD[ HH:MM:SS[.ffffff]]`.","Ensure the application writes proper DATETIME values, not free-form strings.","Tighten sql_mode on the server (STRICT_TRANS_TABLES) so malformed values cannot be stored."],"exampleFix":"// before: column holds '2024/01/01 00:00:00'\nSELECT dt FROM t; // parseDateTime fails on '/' at b[4]\n// after: normalize the data\nUPDATE t SET dt = REPLACE(dt, '/', '-') WHERE dt LIKE '____/__/__%';","handlingStrategy":"validation","validationCode":"if len(s) >= 5 && s[4] != '-' {\n    return fmt.Errorf(\"datetime %q has bad year-month separator\", s)\n}","typeGuard":"func looksLikeDatetime(s string) bool { return len(s) >= 10 && s[4] == '-' && s[7] == '-' }","tryCatchPattern":null,"preventionTips":["Enforce STRICT sql_mode to keep DATETIME columns well-formed.","Validate date strings before insert.","Audit imported data for separator consistency."],"tags":["parsing","time","data"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}