如何用我自己的时间部分格式化日期 SQL
How to format a date with my own time part in SQL
我正在处理一个 SQL 查询,其中 returns 一个整数,它是两个给定日期之间的分钟数,如下所示
DATEDIFF(mi, date_one, getdate())
上面的查询 returns 两个日期相差几分钟但是对于 getdate() 我想提供我自己的时间。
例如,考虑
date_one= 2015-12-29 13:39:03.000
getdate() return current date and time ie., 2015-12-29 14:33:50.000
但是,我想将 getdate() 中的时间部分更改为一些 10:00:00.00
,以便通过传递一个小时整数 10
来使 getdate() 为 2015-12-29 10:00:00.00
。
请问有什么好的方法吗?
我觉得这个功能很有用:
CREATE FUNCTION [dbo].[StripTimeFromDateTime]
(
@date DateTime
)
RETURNS DateTime
AS
BEGIN
RETURN DATEADD(dd, DATEDIFF(dd, 0, @date), 0)
END
这会将时间从日期时间中剔除,并将其保留在 00:00:00.000
。那么你可以:
SELECT DATEADD(hour, 10, dbo.StripTimeFromDateTime(GetDate()))
注意下面的例子:
select
cast('2015-12-28 12:15:00' as datetime),
getdate(),
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime);
|----------------------------|----------------------------|----------------------------|
| December, 28 2015 12:15:00 | December, 29 2015 20:42:35 | December, 29 2015 10:00:00 |
一个类似于您使用的示例:
with example as (
select cast('2015-12-28 12:15:00' as datetime) as date_one
)
select
date_one,
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime) as myown,
datediff(
mi,
date_one,
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime)
) as minutes
from example;
结果:
| date_one | myown | minutes |
|----------------------------|----------------------------|---------|
| December, 28 2015 12:15:00 | December, 29 2015 10:00:00 | 1305 |
SQLFiddle 示例:http://sqlfiddle.com/#!3/9eecb7/6599
我使用 varchar 的原因是可以灵活地输入时间,例如“10:15:00”或其他时间变体。
这将使用 getDate,但您可以设置自己的时间。只需替换第二个参数(10 为您想要的任何小时)。在 dateDiff 函数中使用此表达式代替 getDate()。
DATEADD(hh, 10, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
您还可以添加分钟、秒、毫秒等来获得您需要的。
这里我加了633分钟使10:33(把第一个参数改成mi分钟)
select DATEADD(mi, 633, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
有关第一个参数的其他值,请参阅文档:https://msdn.microsoft.com/en-us/library/ms186819.aspx
使用方法如下:
DATEDIFF(mi, getDate(),
DATEADD(mi, 633, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
)
这将为您提供从当前时间到当天 10:33 的分钟数。这是一个 sqlfiddle:http://sqlfiddle.com/#!6/9eecb7/5407
这 one-liner 将给出当前日期,时间部分替换为您想要的常量
select cast(cast(getdate() as date) as datetime) + cast(cast('10:00:00' as time) as datetime)
这是如何工作的:
将 getdate() 结果转换为 date
,然后返回 datetime
以获取不带时间的当前日期。
select cast(cast(getdate() as date) as datetime)
将“10:00:00”转换为 time
,然后转换为 datetime
以获得 10:00:00 作为日期时间。
select cast(cast('10:00:00' as time) as datetime)
将两者相加
select cast(cast(getdate() as date) as datetime) + cast(cast('10:00:00' as time) as datetime)
就这些了
select DATEADD (hh,10, CONVERT(Datetime, CONVERT (date, GETDATE())))
先删除时间,然后添加 10 小时。
我正在处理一个 SQL 查询,其中 returns 一个整数,它是两个给定日期之间的分钟数,如下所示
DATEDIFF(mi, date_one, getdate())
上面的查询 returns 两个日期相差几分钟但是对于 getdate() 我想提供我自己的时间。
例如,考虑
date_one= 2015-12-29 13:39:03.000
getdate() return current date and time ie., 2015-12-29 14:33:50.000
但是,我想将 getdate() 中的时间部分更改为一些 10:00:00.00
,以便通过传递一个小时整数 10
来使 getdate() 为 2015-12-29 10:00:00.00
。
请问有什么好的方法吗?
我觉得这个功能很有用:
CREATE FUNCTION [dbo].[StripTimeFromDateTime]
(
@date DateTime
)
RETURNS DateTime
AS
BEGIN
RETURN DATEADD(dd, DATEDIFF(dd, 0, @date), 0)
END
这会将时间从日期时间中剔除,并将其保留在 00:00:00.000
。那么你可以:
SELECT DATEADD(hour, 10, dbo.StripTimeFromDateTime(GetDate()))
注意下面的例子:
select
cast('2015-12-28 12:15:00' as datetime),
getdate(),
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime);
|----------------------------|----------------------------|----------------------------|
| December, 28 2015 12:15:00 | December, 29 2015 20:42:35 | December, 29 2015 10:00:00 |
一个类似于您使用的示例:
with example as (
select cast('2015-12-28 12:15:00' as datetime) as date_one
)
select
date_one,
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime) as myown,
datediff(
mi,
date_one,
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime)
) as minutes
from example;
结果:
| date_one | myown | minutes |
|----------------------------|----------------------------|---------|
| December, 28 2015 12:15:00 | December, 29 2015 10:00:00 | 1305 |
SQLFiddle 示例:http://sqlfiddle.com/#!3/9eecb7/6599
我使用 varchar 的原因是可以灵活地输入时间,例如“10:15:00”或其他时间变体。
这将使用 getDate,但您可以设置自己的时间。只需替换第二个参数(10 为您想要的任何小时)。在 dateDiff 函数中使用此表达式代替 getDate()。
DATEADD(hh, 10, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
您还可以添加分钟、秒、毫秒等来获得您需要的。
这里我加了633分钟使10:33(把第一个参数改成mi分钟)
select DATEADD(mi, 633, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
有关第一个参数的其他值,请参阅文档:https://msdn.microsoft.com/en-us/library/ms186819.aspx
使用方法如下:
DATEDIFF(mi, getDate(),
DATEADD(mi, 633, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
)
这将为您提供从当前时间到当天 10:33 的分钟数。这是一个 sqlfiddle:http://sqlfiddle.com/#!6/9eecb7/5407
这 one-liner 将给出当前日期,时间部分替换为您想要的常量
select cast(cast(getdate() as date) as datetime) + cast(cast('10:00:00' as time) as datetime)
这是如何工作的:
将 getdate() 结果转换为 date
,然后返回 datetime
以获取不带时间的当前日期。
select cast(cast(getdate() as date) as datetime)
将“10:00:00”转换为 time
,然后转换为 datetime
以获得 10:00:00 作为日期时间。
select cast(cast('10:00:00' as time) as datetime)
将两者相加
select cast(cast(getdate() as date) as datetime) + cast(cast('10:00:00' as time) as datetime)
就这些了
select DATEADD (hh,10, CONVERT(Datetime, CONVERT (date, GETDATE())))
先删除时间,然后添加 10 小时。