SQL 添加日期时间 添加小时 添加分钟
在我的表格中,我有 DateTime
、Hour
列。
示例:2012-05-14 00:00:00.000
和 1230
如何将此 hour
列添加到我的 Datetime 列,所以我可以
2012-05-14 12:30:00.000
尝试一下:
SELECT DATE_DEBUT, HEURE_DEBUT,
DATEADD(hour, CONVERT(int, SUBSTRING(HEURE_DEBUT, 0, 2)), DATE_DEBUT) AS DateTemp,
DATEADD(hour, CONVERT(int, SUBSTRING(HEURE_DEBUT, 2, 2)), DateTemp) AS DateComplete
FROM ESPTEMPS_PROGRAMMATION
但它不起作用。
预先感谢您, 斯蒂夫
In my table I have DateTime
, Hour
column.
example : 2012-05-14 00:00:00.000
and 1230
How can I add this hour
column into my Datetime
column, so I can have
2012-05-14 12:30:00.000
I tried with this:
SELECT DATE_DEBUT, HEURE_DEBUT,
DATEADD(hour, CONVERT(int, SUBSTRING(HEURE_DEBUT, 0, 2)), DATE_DEBUT) AS DateTemp,
DATEADD(hour, CONVERT(int, SUBSTRING(HEURE_DEBUT, 2, 2)), DateTemp) AS DateComplete
FROM ESPTEMPS_PROGRAMMATION
but it does not work.
thanks you in advance,
Stev
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您希望将前两位数字添加为小时,后两位数字添加为分钟 - 但您没有在
DATEADD
调用中执行此操作 - 您将这两个部分添加为HOUR
- 试试这个:这里我使用两个嵌套的
DATEADD
- 内部DATEADD
添加小时,外部将分钟添加到结果中添加小时数。另外:SQL Server 中的
SUBSTRING
是基于1,例如,字符串的第一个字符位于位置1(而不是您似乎假设的0)From what I understand, you want to add the first two digits as hour, the second two as minute - but you're not doing this in your
DATEADD
calls - you're adding both parts asHOUR
- try this instead:Here I'm using two nested
DATEADD
- the innerDATEADD
adds the hours, the outer adds the minutes onto the result of adding the hours.Also:
SUBSTRING
in SQL Server is 1-based, e.g. the first character of a string is at position 1 (not 0, as you seem to assume)