【问题标题】:Replace 0 in one field in case statement result that is stringing fields在字符串字段的case语句结果中替换一个字段中的0
【发布时间】:2019-06-30 18:28:16
【问题描述】:

我有如下声明:

select distinct s.field1, s.field 2, 

Case when s.intfield3 = 2018 then (RTRIM(cast(s.intfield4 as char (2))) +'/1/'+ cast(s.intfield3 as char (4)))

when c.intfield3 = 2018 then (RTRIM(cast(c.intfield4 as char(2))) +'/1/'+ cast(c.intfield3 as char (4)))
End as newfield

我的问题是 s.intfield4 和 c.intfield4 有一些 0 值。我需要将那些 0 值更改为 12。我尝试了一些方法,但没有任何效果。

【问题讨论】:

  • 标记您正在使用的 DBMS(即MySQLSQL-Server 等)。

标签: sql replace case


【解决方案1】:

你可以使用

case when x = 0 then 12 else x end

【讨论】:

  • 我应该把它放在那个长长的修剪、转换和字符串字段和字符字符串的哪里?它已经在 Case 语句中。
  • @PurpleCyn 。 . . “我的问题是 s.intfield4 和 c.intfield4 有一些 0 值。我需要将这些 0 值更改为 12。”我相信这回答了你的问题。
  • 我想多了!谢谢!
  • 对于后面的其他人,也想多了:select distinct s.field1, s.field 2, Case when s.intfield3 = 2018 and s.intfield4 > 0 then (RTRIM(cast(s .intfield4 as char (2))) +'/1/'+ cast(s.intfield3 as char (4))) when s.intfield3 = 2018 and s.intfield4 = 0 then ('12/1/'+ cast (s.intfield3 as char (4))) 等等......
猜你喜欢
  • 2019-10-04
  • 2017-01-08
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2014-05-07
  • 1970-01-01
相关资源
最近更新 更多