SELECT CONVERT(VARCHAR(10), GETDATE(), 120) -- get date as text truncating any timeSELECT CAST(CONVERT(VARCHAR(10), GETDATE(), 120) AS DATETIME)-- So if you want 12pm (noon) - you would add that in the text phaseSELECT CAST(CONVERT(VARCHAR(10), GETDATE(), 120) + ' 12:00:00' AS DATETIME)-- Note the space in the time: ' 12:00:00'
SELECT DATEADD(DAY, 0, DATEDIFF(DAY, 0, theDate)),CAST(CAST(theDate AS DATE) AS DATETIME)FROM (VALUES(GETDATE())) a(theDate);
SELECT [StartDateTime], [EndDateTime], [RunTime(Minutes)]INTO #sampleDataFROM (VALUES('2012-09-30 12:35:26.033','2012-09-30 12:51:05.170',15.650000), ('2012-09-29 16:27:35.957','2012-09-29 16:43:45.987',16.166666), ('2012-09-28 20:07:19.493','2012-09-28 20:23:33.343',16.233333), ('2012-09-27 01:05:07.513','2012-09-27 01:20:00.410',14.883333), ('2012-09-20 11:11:27.083','2012-09-20 11:29:00.037',17.550000), ('2012-09-19 17:56:50.700','2012-09-19 18:15:17.540',18.450000) )a([StartDateTime], [EndDateTime], [RunTime(Minutes)]);
SELECT [StartDateTime], [EndDateTime], [DiffInMilliSeconds]/60000.0 AS [RunTime(Minutes)]FROM (SELECT CAST(CAST([EndDateTime] AS DATE) AS DATETIME), [EndDateTime], DATEDIFF(MS,CAST(CAST([EndDateTime] AS DATE) AS DATETIME),[EndDateTime]) FROM #sampleData) a([StartDateTime], [EndDateTime], [DiffInMilliSeconds]);
DECLARE @Table TABLE (SD DATETIME, ED DATETIME)INSERT INTO @Table VALUES('2012-09-30 12:35:26.033','2012-09-30 12:51:05.170'),-- 15.650000('2012-09-29 16:27:35.957','2012-09-29 16:43:45.987'),-- 16.166666('2012-09-28 20:07:19.493','2012-09-28 20:23:33.343'),-- 16.233333('2012-09-27 01:05:07.513','2012-09-27 01:20:00.410'),-- 14.883333('2012-09-20 11:11:27.083','2012-09-20 11:29:00.037'),-- 17.550000('2012-09-19 17:56:50.700','2012-09-19 18:15:17.540')-- 18.450000SELECT * FROM @TableSELECT SD = DATEADD(DAY, 0, DATEDIFF(DAY, 0, SD)), ED, [DIFFERENCE] = DATEDIFF(MINUTE,DATEADD(DAY, 0, DATEDIFF(DAY, 0, SD)), ED) FROM @Table
SD | ED | DIFFERENCE2012-09-30 00:00:00.000 | 2012-09-30 12:51:05.170 | 7712012-09-29 00:00:00.000 | 2012-09-29 16:43:45.987 | 10032012-09-28 00:00:00.000 | 2012-09-28 20:23:33.343 | 12232012-09-27 00:00:00.000 | 2012-09-27 01:20:00.410 | 802012-09-20 00:00:00.000 | 2012-09-20 11:29:00.037 | 6892012-09-19 00:00:00.000 | 2012-09-19 18:15:17.540 | 1095
SELECT [StartDateTime], [EndDateTime], [RunTime(Minutes)]INTO #sampleDataFROM (VALUES('2012-10-01 12:35:26.033','2012-09-30 12:51:05.170',15.650000), ('2012-10-01 16:27:35.957','2012-09-29 16:43:45.987',16.166666), ('2012-10-01 20:07:19.493','2012-09-28 20:23:33.343',16.233333), ('2012-10-01 01:05:07.513','2012-09-27 01:20:00.410',14.883333), ('2012-10-01 11:11:27.083','2012-09-20 11:29:00.037',17.550000), ('2012-10-01 17:56:50.700','2012-09-19 18:15:17.540',18.450000) )a([StartDateTime], [EndDateTime], [RunTime(Minutes)]);SELECT * ,NewStartDT=CAST(STUFF( StartDatetime, 1, 23 , SUBSTRING(EndDateTime, 1, 10) + ' 12:00:00.000') AS DATETIME) ,NewStartDT2=DATEADD(day, DATEDIFF(day, 0, EndDateTime), 0)+0.5FROM #sampleDataDROP TABLE #sampleData