Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Eliminate time

Author  Topic 

svallamreddy
Starting Member

4 Posts

Posted - 2008-03-12 : 05:42:39
Hi,

In my query where clause I am using between to get data. Because of time in the data I need to eliminate that and compare, how can I eliminate. my where clause is as below. due to that my query performance is falling down. please help.

CONVERT(DATETIME, CONVERT(CHAR(20), OpportunityDate,110)) BETWEEN CONVERT(CHAR(20),@FDate,110) AND CONVERT(CHAR(20),@TDate,110)

Thanks

Sreenu

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-03-12 : 05:47:37
don't convert to char for this...

DATEADD(DAY,DATEDIFF(DAY,0,OpportunityDate),0)
BETWEEN DATEADD(DAY,DATEDIFF(DAY,0,@FDate),0)
and DATEADD(DAY,DATEDIFF(DAY,0,@TDate),0)


Em
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-03-12 : 05:49:50
actually... do the variables you're passing have a time as well? if not then just...

DATEADD(DAY,DATEDIFF(DAY,0,OpportunityDate),0)
BETWEEN @FDate and @TDate


Em
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-12 : 05:54:23
Don't even touch the OpportunityDate column!
It makes it impossible to utilize a present index.

And style 110 is a very bad choice!

Look here, if you want all records dated May 15, 2007 through July 9, 2007 use this

SET @fDate = '20070515'
SET @tDate = '20070710'

SELECT *
FROM Table1
WHERE OpportunityDate >= @fDate
AND OpportunityDate < @TDate



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-12 : 05:57:50
Of, if the @tDate is passed as "July 9, 2007" from the client application, use

SELECT *
FROM Table1
WHERE OpportunityDate >= @fDate
AND OpportunityDate < dateadd(day, 1, @TDate)


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-03-12 : 06:11:11
Is that better, worse or no different to...

SELECT *
FROM table1
WHERE
DATEDIFF(DAY, @fDate, OpportunityDate) >= 0
AND DATEDIFF(DAY, opportunityDate, @TDate) >= 0


-------------
Charlie
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-12 : 06:15:00
Whenever you make a calculation over an indexed column, it renders the index useless.

So you suggestion is as bas as the others, performance wise.
Logically they are the same, ie they return the same result, only slower...


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

svallamreddy
Starting Member

4 Posts

Posted - 2008-03-12 : 07:04:39
Dear Thank you so much,

Its excellet,

Thanks

Sreenu

quote:
Originally posted by Peso

Of, if the @tDate is passed as "July 9, 2007" from the client application, use

SELECT *
FROM Table1
WHERE OpportunityDate >= @fDate
AND OpportunityDate < dateadd(day, 1, @TDate)


E 12°55'05.25"
N 56°04'39.16"


Go to Top of Page
   

- Advertisement -