I want to convert :
select convert(nvarchar(16), TimeLog, 120) requestTime,
count(Request) As noOfQueries
from LogData
where TimeLog between @StartDate and @EndDate
group by convert(nvarchar(16), TimeLog, 120) order by requestTime;
Here in the where clause the Timelog is between: 2012-06-21 and 2012-06-21 but I want it between 2012-06-21 00:00:00 and 2012-06-21 23:59:59
Hence I want to convert the @Satrtdate / @EndDate into above format so I would like to append 00:00:00 to @StartDate and 23:59:59 to @EndDate .
What you want is:
The reason you don’t want to use
BETWEENis because its behavior will differ depending on the underlying data type. If it’sSMALLDATETIME, you will round up, and get data from the next day. If it’sDATETIME2, you could miss data between23:59:59.0000000 and 23:59:59.9999999. And even if you know the data type now, it could change after you publish your query. Who’s going to go back and correct23:59:59to23:59:00or23:59:59.9999999? With an open-ended range, selecting everything before the next day at midnight, you’ll never have to worry about it.Please give these two articles a read:
What do BETWEEN and the devil have in common?
Bad habits to kick : mis-handling date / range queries