SQL添加日期时间添加小时添加分钟 [英] SQL add Datetime add hour add Minute
问题描述
在我的表中,我有 DateTime
, Hour
列。
In my table I have DateTime
, Hour
column.
示例: 2012-05-14 00:00:00.000
和 1230
如何将此 hour
列添加到 Datetime
列中,所以我可以有
How can I add this hour
column into my Datetime
column, so I can have
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
但它不起作用。
预先感谢您,
Stev
thanks you in advance, Stev
推荐答案
据我了解,您想将前两位数字添加为小时,将后两位数字添加为分钟-但您没有在 DATEADD
调用中执行此操作-您将两个部分都添加为 HO UR
-尝试以下操作:
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 as HOUR
- try this instead:
SELECT DATE_DEBUT, HEURE_DEBUT,
DATEADD(MINUTE, CONVERT(int, SUBSTRING(HEURE_DEBUT, 3, 2)),
DATEADD(HOUR, CONVERT(int, SUBSTRING(HEURE_DEBUT, 1, 2)), DATE_DEBUT))
FROM ESPTEMPS_PROGRAMMATION
这里我使用两个嵌套的 DATEADD
-内部的 DATEADD
添加小时,外部添加分钟到添加小时的结果上。
Here I'm using two nested DATEADD
- the inner DATEADD
adds the hours, the outer adds the minutes onto the result of adding the hours.
也:<$ c $ SQL Server中的c> SUBSTRING 是基于 1的,例如字符串的第一个字符位于位置1(似乎不假设为0)
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)
这篇关于SQL添加日期时间添加小时添加分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!