我的 SQL 代码显示 00 而不是上午 12 点。我该如何调整这个?
我有以下代码,这对我来说非常有用。我在 SQL 的 SSIS 包中使用它。我遇到的问题是,在午夜和凌晨 1:00 之间,时间显示为 00。我想尽可能保留以下代码,因为它对我有用,但只需进行必要的调整。任何帮助将不胜感激!
谢谢你, 约翰
SELECT CASE WHEN DATEPART(hour, DateTimeField) < 13 THEN
RIGHT(REPLICATE(' ', 2) + CAST(datepart(hour, DateTimeField) AS VARCHAR(2)), 2)
ELSE
RIGHT(REPLICATE(' ', 2) + CAST(datepart(hour, dateadd(hour, - 12, DateTimeField))
AS VARCHAR(2)), 2) END
I have the following code which is working great for me. I am using it in a SSIS package in SQL. The issue i am having is that between midnight and 1:00 AM, the hour is showing up as 00. I would like to stay with the following code as much as possible, as it is working for me, but just make the needed adjustement. Any help would be greatly apprecieated!!!
Thank you,
John
SELECT CASE WHEN DATEPART(hour, DateTimeField) < 13 THEN
RIGHT(REPLICATE(' ', 2) + CAST(datepart(hour, DateTimeField) AS VARCHAR(2)), 2)
ELSE
RIGHT(REPLICATE(' ', 2) + CAST(datepart(hour, dateadd(hour, - 12, DateTimeField))
AS VARCHAR(2)), 2) END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以简单地这样做:
I think you could simply do this instead:
假设是Oracle,因为你没有指定。一般技术是使用函数
TO_CHAR
和允许 01-12 而不是 00-23 的日期转换模板。Assuming Oracle, since you did not specify. The general technique is to use the function
TO_CHAR
and a date conversion template that allows 01-12 instead of 00-23.