Case When 语句的查询
在我插入 casewhen 语句期间,由于我的 casewhen,oracle 会自动在任何固定数字后面插入另一个“0”。
例如:
INSERT INTO test
(test_date,
testno,
hours)
SELECT '20-OCT-2010',
'1234',
CASE
WHEN Extract(DAY FROM( endtime - starttime )) >= 1 THEN (
Extract(DAY FROM(
endtime - starttime ))
* 24 + Extract
(HOUR FROM(
endtime - starttime
)) )
WHEN starttime IS NULL
AND endtime IS NULL THEN 0
ELSE ( Extract(HOUR FROM( endtime - starttime )) )
END
||''
|| CASE
WHEN endtime IS NULL
AND starttime IS NULL THEN 0
ELSE Extract(MINUTE FROM ( endtime - starttime )) / 60
END AS hours
FROM testtime
我从上述语句执行的结果:
Test_Date TestNo Hours
20-OCT-2010 1234 140
预期结果:
Test_Date TestNo Hours
20-OCT-2010 1234 14
我的小时数据类型是数字 (4,2) 我的错误是由于如果分钟的开始时间和结束时间为空,它会自动在其后面添加“0”。即使我将'0'更改为null,它仍然会在后面添加0。
我可以知道是否有另一种方法来放置“0”值?
谢谢
During my insertion of case when statement, the oracle will auto insert another '0' behind any fixed number due to my case when.
For example:
INSERT INTO test
(test_date,
testno,
hours)
SELECT '20-OCT-2010',
'1234',
CASE
WHEN Extract(DAY FROM( endtime - starttime )) >= 1 THEN (
Extract(DAY FROM(
endtime - starttime ))
* 24 + Extract
(HOUR FROM(
endtime - starttime
)) )
WHEN starttime IS NULL
AND endtime IS NULL THEN 0
ELSE ( Extract(HOUR FROM( endtime - starttime )) )
END
||''
|| CASE
WHEN endtime IS NULL
AND starttime IS NULL THEN 0
ELSE Extract(MINUTE FROM ( endtime - starttime )) / 60
END AS hours
FROM testtime
My results executed from the above statement:
Test_Date TestNo Hours
20-OCT-2010 1234 140
Expected results:
Test_Date TestNo Hours
20-OCT-2010 1234 14
My data type for hours is number (4,2)
My error is due to that if the starttime and endtime for minute is null, it will auto add an '0' behind it. Even i change the '0' to null, it will still add 0 behind.
May i know is there another way to place the '0' value?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应将小时数和分钟数除以六十连接起来,而应将它们相加。例如,3 小时 0 分钟是
3 + 0 = 3
小时,而不是3 || 0 = 30 小时。
换句话说,将
||''||
更改为+
。Instead of concatenating number-of-hours and number-of-minutes,-divided-by-sixty, you should add them. For example, 3 hours and 0 minutes is
3 + 0 = 3
hours, not3 || 0 = 30
hours.In other words, change your
||''||
to+
.