# sql：第2列扫描错误：不支持将NULL转换为字符串

- **ID:** `go/database-sql-null-value`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

将数据库中的NULL列扫描到不可为空的Go类型（如string）中，而未使用sql.NullString。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.19 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   var s sql.NullString
if err := rows.Scan(&s); err != nil {
    // handle error
}
if s.Valid {
    fmt.Println(s.String)
}
   ```
2. **** (80% 成功率)
   ```
   Use COALESCE in SQL query to convert NULL to default value.
   ```

## 无效尝试

- **** — NULL is not the same as an empty string and can lead to data loss. (70% 失败率)
- **** — Go does not allow direct casting of NULL to string. (90% 失败率)
