Rounding dates to first day of the month
Use FORMAT
to format your date.
DECLARE @date DATETIME = '2015-10-17 00:00:00.000'
SELECT FORMAT(@date, 'yyyy-MM-01 HH:mm:ss.fff')
Or if you don't want time part:
SELECT FORMAT(@date, 'yyyy-MM-01 00:00:00.000')
LiveDemo
If, for example, it is 15th of given month then you subtract 14 and cast the result to date:
SELECT ArrivalDate
, CAST(DATEADD(DAY, -DATEPART(DAY, ArrivalDate) + 1, ArrivalDate) AS DATE) AS FirstDay
FROM (VALUES
(CURRENT_TIMESTAMP)
) AS t(ArrivalDate)
ArrivalDate | FirstDay
2019-05-15 09:35:12.050 | 2019-05-01
But my favorite is EOMONTH
which requires SQL Server 2012:
SELECT ArrivalDate
, DATEADD(DAY, 1, EOMONTH(ArrivalDate, -1)) AS FirstDay
FROM (VALUES
(CURRENT_TIMESTAMP)
) AS t(ArrivalDate)
ArrivalDate | FirstDay
2019-05-15 09:35:52.657 | 2019-05-01