SELECT t1.id, STUFF((SELECT ',' + CONVERT(VARCHAR, CASE WHEN ISNUMERIC(REPLACE(t2.hsdj, ',', '')) = 1 THEN CAST(REPLACE(t2.hsdj, ',', '') AS FLOAT) ELSE NULL END) FROM formtable_main_43_dt1 t2 WHERE t1.id = t2.mainid FOR XML PATH ('')), 1, 1, '') AS MergedValues, STUFF((SELECT ',' + CAST(CAST(t1.htwj AS VARBINARY(MAX)) AS VARCHAR(MAX)) FROM formtable_main_43 t1 WHERE t1.id = t2.mainid FOR XML PATH ('')), 1, 1, '') AS ImageValues FROM formtable_main_43 t1 INNER JOIN formtable_main_43_dt1 t2 ON t1.id = t2.mainid GROUP BY t1.id, t1.htwj;提示不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符
时间: 2023-12-16 15:03:00 浏览: 89
sql.rar_exec select_select_sql select from exec_普通sql大全
根据你提供的 SQL 语句和错误提示,应该是 `CAST(CAST(t1.htwj AS VARBINARY(MAX)) AS VARCHAR(MAX))` 这部分出现了问题。因为 `htwj` 列的数据类型是 text、ntext 或 image,所以无法直接进行转换。你可以使用 `CONVERT` 函数将其转换为 `varchar(max)` 类型,如下所示:
```
SELECT t1.id, STUFF((SELECT ',' + CONVERT(VARCHAR, CASE WHEN ISNUMERIC(REPLACE(t2.hsdj, ',', '')) = 1 THEN CAST(REPLACE(t2.hsdj, ',', '') AS FLOAT) ELSE NULL END) FROM formtable_main_43_dt1 t2 WHERE t1.id = t2.mainid FOR XML PATH ('')), 1, 1, '') AS MergedValues, STUFF((SELECT ',' + CONVERT(VARCHAR(MAX), t1.htwj) FROM formtable_main_43 t1 WHERE t1.id = t2.mainid FOR XML PATH ('')), 1, 1, '') AS ImageValues FROM formtable_main_43 t1 INNER JOIN formtable_main_43_dt1 t2 ON t1.id = t2.mainid GROUP BY t1.id, t1.htwj;
```
这样就可以将 `htwj` 列转换为 `varchar(max)` 类型,避免出现错误。
阅读全文