4

我需要从时间戳列四舍五入到最近的月初(时间戳格式)。

如何做到这一点?

例子:

TimestampColumn A:             Rounded to these values
2012-01-07 18:18:29.923        2012-01-01 00:00:00.000
2012-01-14 12:58:13.122        2012-01-01 00:00:00.000
2012-06-09 17:10:30.787        2012-06-01 00:00:00.000
2012-05-31 09:29:43.870        2012-06-01 00:00:00.000
2012-10-22 12:09:47.067        2012-11-01 00:00:00.000
2012-10-15 04:35:11.013        2012-10-01 00:00:00.000
4

6 回答 6

2

考虑先转换为日期

DECLARE @d DATETIME
set @d = CONVERT(DATE, '2012-02-14 12:58:13.122')

SET @d =  DATEADD(DAY, 1-datepart(day, @d), @d)

SELECT @d
于 2012-12-03T19:46:01.867 回答
0

这是一个例子。您有 5 个字段:您要转换的日期、您想要的转换日期、当月的第一天、当月的最后一天和下个月的第一天。只需选择您需要的内容:

SELECT
    BED_Meeting_When,
    CASE WHEN DAY(BED_Meeting_When) < 15 THEN (DATEADD(DAY, (-DAY(BED_Meeting_When) + 1), BED_Meeting_When)) ELSE DATEADD(DAY, (-DAY(BED_Meeting_When) + 1), DATEADD(MONTH, 1, BED_Meeting_When)) END,
    DATEADD(DAY, (-DAY(BED_Meeting_When) + 1), BED_Meeting_When) AS 'Arrondi au premier du mois',
    DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, BED_Meeting_When) + 1, 0)) AS 'Arrondi au dernier du mois',
    DATEADD(DAY, (-DAY(BED_Meeting_When) + 1), DATEADD(MONTH, 1, BED_Meeting_When)) AS 'Arrondi au premier du mois suivant',
    BEMR_Titre
FROM bpri_entretien_detail
    INNER JOIN bpri_entretien_motif ON (BED_BEMR_Idx = BEMR_Idx)
于 2015-02-04T09:02:27.400 回答
0

这是一种方法 - 只需减去您不关心的日期的所有部分:

DECLARE @d DATETIME
set @d = '2012-02-14 12:58:13.122'

SET @d =  DATEADD(DAY, 1-datepart(day, @d), @d)
SET @d =  DATEADD(hour, -datepart(hour, @d), @d)
SET @d =  DATEADD(minute, -datepart(minute, @d), @d)
SET @d =  DATEADD(second, -datepart(second, @d), @d)
SET @d =  DATEADD(millisecond, -datepart(millisecond, @d), @d)

SELECT @d
于 2012-12-02T01:35:43.840 回答
0

感谢您的贡献。但我会选择这个;它足够好。

SELECT CASE
WHEN DATEDIFF(DAY, GETDATE(), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)) > 
ABS(DATEDIFF(DAY, GETDATE(), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))) 
THEN DATEADD(dd, datediff(dd, 0, DATEADD(DAY, DATEDIFF(DAY, GETDATE(), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)), GETDATE() ) )+0, 0)
ELSE dateadd(dd, datediff(dd, 0, DATEADD(DAY, DATEDIFF(DAY, GETDATE(), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)), GETDATE() ) )+0, 0)
END
于 2012-12-02T03:40:58.257 回答
0

以秒为单位计算适用月份的长度,然后确定您是否已过了月中。根据需要前进或后退。

declare @Foo as DateTime = '2012-10-15 12:35:11.013'

select
  DateAdd( month, DateDiff( m, 0, @Foo ), 0 ) as 'Year/Month',
  DateDiff( s, DateAdd( month, DateDiff( m, 0, @Foo ), 0), @Foo ) as 'Seconds Into Month',
  DateDiff( s, DateAdd( month, DateDiff( m, 0, @Foo ), 0 ), DateAdd( month, DateDiff( m, 0, @Foo ) + 1, 0 ) ) as 'Seconds In Month',
  DateDiff( s, DateAdd( month, DateDiff( m, 0, @Foo ), 0 ), DateAdd( month, DateDiff( m, 0, @Foo ) + 1, 0 ) ) / 2 as 'Seconds In Half Month',
  DateAdd( month, DateDiff( m, 0, @Foo ) + Round( 1.0 * DateDiff( s, DateAdd( month, DateDiff( m, 0, @Foo ), 0), @Foo ) / DateDiff( s, DateAdd( month, DateDiff( m, 0, @Foo ), 0 ), DateAdd( month, DateDiff( m, 0, @Foo ) + 1, 0 ) ), 0 ), 0 ) as 'Rounded Date'
于 2012-12-02T03:29:44.493 回答
0

嗨,我知道聚会迟到了,一点简单的提议永远不会冒犯我希望

创建一个迷你临时表我弹出了原始帖子中提供的日期

然后从中选择如下

创建表#DT(

TS_A 日期时间,TS_B 日期时间)

插入#DT (ts_a) 值 ('2012-01-07 18:18:29.923'), ('2012-01-14 12:58:13.122'), ('2012-06-09 17:10:30.787 '), ('2012-05-31 09:29:43.870'), ('2012-10-22 12:09:47.067'), ('2012-10-15 04:35:11.013')

选择 TS_A, DATEADD(MONTH, DATEDIFF(MONTH, 0,ts_a), 0) TS_B from #DT

结果 - 我希望这很清楚

结果

令人惊讶的是,他们是如何变化的:-)

TX
我希望这一切都好 十多年来没有在任何地方发布过

于 2022-02-10T06:46:43.403 回答