【问题标题】:How to update column with case statement and typecast?如何使用案例语句和类型转换更新列?
【发布时间】:2022-10-16 21:34:08
【问题描述】:

我已经尝试了以下,但它给出了这个错误

ERROR:  column "rating" is of type numeric but expression is of type text
LINE 2: set rating = CASE                     ^
HINT:  You will need to rewrite or cast the expression.
    update rating  
    set cast rating as varchar = CASE 
    WHEN rating<0 THEN '0'  
    When rating>3.5 THEN '4'  
    When rating is null THEN '-1'    
    End 

【问题讨论】:

  • 错误说明了一切,评级是数字,所以去掉你的数字周围的引号
  • “结构化查询语言 (SQL) 是一种用于查询数据库的语言。问题应包括代码示例、表结构、示例数据和 DBMS 实现的标记(例如 MySQL、PostgreSQL、Oracle、MS SQL Server、IBM DB2 等)。 "

标签: sql sql-update case


【解决方案1】:

您是否尝试过将文本明确转换为数字?

update rating  
set cast rating as varchar = CASE 
WHEN rating<0 THEN '0'::numeric
When rating>3.5 THEN '4'::numeric
When rating is null THEN '-1'::numeric
End 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多