Thursday, January 12, 2012

First day has a time of 00:00:00 and the last day has a time of 23:59:59.

DECLARE @FromDate datetimeDECLARE @ToDate datetime
SET @FromDate = GETDATE()
SET @ToDate = GETDATE()
Print '------------------------ '
PRINT @FromDatePRINT @ToDateSET @FromDate = CONVERT(DATETIME, CONVERT(varchar(11),@FromDate, 111 ) + ' 00:00:00', 111) SET @ToDate = CONVERT(DATETIME, CONVERT(varchar(11),@ToDate, 111 ) + ' 23:59:59', 111)
Print '------------------------ '
PRINT @FromDatePRINT @ToDate


DECLARE @TEST_FROM DATETIMESET @TEST_FROM = dateadd(month,((YEAR(@FromDate)-1900)*12)+MONTH(@FromDate)-1,DAY(@FromDate)-1) + ' 12:00:00'
DECLARE @TEST_TO DATETIMESET @TEST_TO = dateadd(month,((YEAR(@ToDate)-1900)*12)+MONTH(@ToDate)-1,DAY(@ToDate)-1) + ' 23:59:59'
Print '------------------------ '
PRINT @TEST_FROMPRINT @TEST_TO


This will print the following in SQL Query editor screen
------------------------ 
Dec 28 2011  3:18PM
Dec 28 2011  3:18PM------------------------ 
Dec 28 2011 12:00AM
Dec 28 2011 11:59PM------------------------ 
Dec 28 2011 12:00PM
Dec 28 2011 11:59PM

No comments:

Post a Comment