sql - 在 ELSE CASE SQL 查询上使用 Convert

标签 sql postgresql case case-when sql-convert

我正在 Postgres 中执行 SQL 查询,它选择一个数字字段。我需要显示一个字符串值作为此 SELECT 的结果,因此我使用了这样的 CASE 语句:

Select 
case numeric_field
when 100 then 'some string'
when 200 then 'some other string'

问题是,如果数字字段有任何其他值(例如 300),我需要显示该值(当然是字符串)。我试着像这样在 else 上放置一个 CONVERT

...
else CONVERT(varchar(10),numeric_field)

但是没有用。我该怎么做?

最佳答案

SELECT CASE numeric_field
          WHEN 100 THEN 'some string'
          WHEN 200 THEN 'some other string'
          ELSE numeric_field::text
       END AS result
  • 您的陈述不完整,缺少ENDRead the manual here.
  • 要将数字字段输出为文本,只需将其转换为文本:numeric_field::text,它是 SQL 标准调用的 Postgres 特定简短形式:

    cast (numeric_field AS text)
    

关于sql - 在 ELSE CASE SQL 查询上使用 Convert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15159868/

相关文章:

mysql - 如何修复mysql中的 "FUNCTION doesnt exist"错误?

mysql - 获取特定字段数据备份以及mysql中的更新查询

sql - 循环分配

database - 在大 PostgreSQL 表上更新查询太慢

postgresql - Postgres连接器 : No topics discovered on this connector

swift - 错误: Expression pattern of type 'CountableClosedRange<Int>' cannot match values of type 'UITextField!'

vb.net - Select Case with "Is"operator

Mysql RAND() 函数基于其他一些值

postgresql - postgres 9.1 日志文件更改

apache - htaccess 使用 OR 和 NC 重定向