|
|
|
This script shows a technique to perform effective date searches especially when searching on an indexed column. |
Click here to copy the following block | CREATE TABLE #Dates ( DateCol datetime ); CREATE CLUSTERED INDEX IX_Dates ON #Dates( DateCol ); INSERT INTO #Dates VALUES( '2000-09-26 10:00:18' ); INSERT INTO #Dates VALUES( '2000-09-26 01:00:23' ); INSERT INTO #Dates VALUES( '2000-09-28 18:20:09' ); INSERT INTO #Dates VALUES( '2000-09-28 03:35:58' ); INSERT INTO #Dates VALUES( '2000-10-20 10:00:47' ); INSERT INTO #Dates VALUES( '2000-10-09 11:41:28' ); INSERT INTO #Dates VALUES( '2000-10-02 06:18:01' ); INSERT INTO #Dates VALUES( '2000-10-06 09:33:10' ); INSERT INTO #Dates VALUES( '2000-10-06 12:45:00' ); SELECT * FROM #Dates;
GO
DECLARE @Start datetime , @End datetime; SELECT @Start = '2000-09-26' , @End = '2000-10-08';
SELECT * FROM #Dates WHERE CONVERT( datetime , CONVERT( varchar , DateCol , 112 ) ) BETWEEN @Start And @End;
SELECT @Start = convert( varchar , @Start , 112 ) , @End = DATEADD( day , 1 , convert( varchar , @End , 112 ) ) ; SELECT @Start AS SearchStart , @End AS SearchEnd;
SELECT * FROM #Dates WHERE DateCol >= @Start And DateCol < @End;
GO DROP TABLE #Dates; GO |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|