SQL 案例总是使用第一个案例?

SQL Case always uses first Case?

所以我有一个 SQl-案例,它询问整数 % 3600 是否超过 10...如果不是,它应该 concat() 前面有一个'0'...与其他情况相同,略有变化...它应该代表一个时间,

结果应该是这样的:

Int_Sek
00:43:23
09:03:45
34:03:09

但是结局是这样的:
Int_Sek
0:43:23
4:3:45
23:3:9

所以它总是使用第一种情况,我不知道为什么...有人知道吗?

Select concat(
case 
when Int_Sek / 3600 <= 9 then concat('0', floor(Int_Sek / 3600)) 
when Int_Sek / 3600 >= 10 then floor(Int_Sek / 3600) END, ':', 
case 
when (Int_Sek % 3600) / 60 <= 9 then concat ('0', (Int_Sek % 3600) / 60) 
when (Int_Sek % 3600) / 60 >= 10 then floor((Int_Sek % 3600) / 60) END, ':', 
case 
when ((Int_Sek % 3600) % 60) % 60 <= 9 then concat('0', ((Int_Sek % 3600) % 60) % 60) 
when ((Int_Sek % 3600) % 60 )% 60 >= 10 then floor(((Int_Sek % 3600) % 60) % 60) END) as "Column A",
from Table A

如果您使用的是 SQL 服务器,为什么不直接转换为 time

select dateadd(second, int_sek, convert(time, 0))

您可以将生成的 time 格式化为字符串。

其他数据库提供类似的功能。

你的代码有什么问题?考虑您的第一个 case 表达式:

case when Int_Sek / 3600 <= 9
     then concat('0', floor(Int_Sek / 3600)) 
     when Int_Sek / 3600 >= 10
     then floor(Int_Sek / 3600) 
end,

第一个 then return 是一个字符串。第二个 return 是一个整数。整体表达式return是什么意思?它 return 是一个整数。因此,领先的 '0' 丢失了。有多种方法可以解决这个问题,但我想最规范的是:

case when Int_Sek / 3600 <= 9
     then concat('0', floor(Int_Sek / 3600)) 
     when Int_Sek / 3600 >= 10
     then cast(floor(Int_Sek / 3600) as varchar(255))
end,

请注意,我可能还建议将其简化为:

concat(case when Int_Sek / 3600 <= 9 then '0' else '' end,
       floor(Int_Sek / 3600), ':',
       . . .