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 03, 2013 2:10 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:04 PM
Points: 1,743, Visits: 2,545
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)
I'm not fat, I'm gravity challenged.
Post #1491087
Posted Wednesday, September 04, 2013 3:53 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 6:35 PM
Points: 22,491, Visits: 30,182
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