首页 > 解决方案 > LIKE 表达式的左侧必须计算为 varchar?

问题描述

我正在编写一个带有基于字段内字符串的过滤器的查询。但是,我正在运行它显示错误的查询:

LIKE 表达式的左侧必须计算为 varchar

我希望每个购买了基于custom_field.

这是自定义字段内的示例数据:

{ "car_model;":"BMWZ4", "car_id":"0980291", "manufacture":"bmw" }

代码:

SELECT
    first_name,
    Last_name,
    country,
    custom_field
FROM 
    sales_table
WHERE 
    custom_field LIKE '%BMWZ4%' 

预期结果

first_name: John   
last_name: Parker      
country: Germany   
custom_field: {"car_model;":"BMWZ4","car_id":"098222291","brand":"bmw"}

标签: sqlsql-like

解决方案


SELECT
first_name,
Last_name,
country,
custom_field
FROM sales_table
WHERE cast( custom_field as varchar(max)) LIKE '%BMWZ4%' 

推荐阅读