Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Dynamic SQL and DATEADD with User Defined Variable in Number/2nd position Expand / Collapse
Author
Message
Posted Tuesday, September 3, 2013 2:10 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:04 PM
Points: 2,102, Visits: 3,164
You don't want to use a function against the END_DATETIME column if you can avoid it, since any function will prevent SQL from using the full seek capabilities against that column in any index(es) in which it might appear.

I think the code below will do SELECT the same rows as your original query, but adjust the code if/as needed to match you need:


SELECT *
FROM dbo.tablename
WHERE
END_DATETIME >= DATEADD(HOUR, -@tzoffset, @StartDate) AND
END_DATETIME <= DATEADD(HOUR, -@tzoffset, @EndDate)




SQL DBA,SQL Server MVP('07, '08, '09)

"We came in spastic, Like tameless horses /
We left in plastic, As numbered corpses / ...
Remember Charlie, Remember Baker /
They left their childhood On every acre /
And who was wrong? And who was right? /
It didn't matter in the thick of the fight." : the inimitable Mr. Billy Joel, about the Vietnam War
Post #1491087
Posted Wednesday, September 4, 2013 3:53 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:22 PM
Points: 20,680, Visits: 32,279
ScottPletcher (9/3/2013)
You don't want to use a function against the END_DATETIME column if you can avoid it, since any function will prevent SQL from using the full seek capabilities against that column in any index(es) in which it might appear.

I think the code below will do SELECT the same rows as your original query, but adjust the code if/as needed to match you need:


SELECT *
FROM dbo.tablename
WHERE
END_DATETIME >= DATEADD(HOUR, -@tzoffset, @StartDate) AND
END_DATETIME <= DATEADD(HOUR, -@tzoffset, @EndDate)




Same thing I suggested in my post.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1491199
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse