我正在 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
- 您的陈述不完整,缺少
END
。 Read 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/