SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


move calculation to right side


move calculation to right side

Author
Message
Wrangler
Wrangler
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 149
I'm having issues with the following query. I'd like to use the index on the Date_Created field so the dateadd needs to move.

Original:
select * from theTable
where
dateadd(day,364,Date_Created)
between DATEADD(day,-8,'2013-12-25') and '2013-12-25' )

Mine:
select * from theTable
where
date_created
between dateadd(day,-373,'2013-12-25') and dateadd(day,-365,'2013-12-25')

Does this look correct?
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17442 Visits: 19122
You have a difference, but it's just by one day.
The easiest way to test this is to run a query against a calendar table that will return all dates and one row per date.

select * from theTable 
where
Date_Created
between dateadd(day,-372,'2013-12-25') and dateadd(day,-364,'2013-12-25')
ORDER BY Date_Created




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Wrangler
Wrangler
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 149
I created the table as suggested and tested. This is what I found worked:

select date_created as new from caltest
where
Date_Created between dateadd(day,-372,'2013-12-25') and dateadd(day,-363,'2013-12-25')
ORDER BY Date_Created


Thank you
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search