July 2, 2013 at 1:56 pm
How I create a function to get all policies that expire a 120 days from now.
Eg If the policy effectivedate is November 1 2012 and expiration date is November 1 2013 then on july first when I run a query this policy should be selected.
July 2, 2013 at 2:01 pm
You're going to want to use the DATEDIFF function.
For something more specific, please post the DDL to create your tables and some sample data so we can see what you see. Ideally, we should be able to copy/paste your SQL to create your tables with minimal data so we can work on it.
July 2, 2013 at 2:18 pm
f9c882q 61644 (7/2/2013)
How I create a function to get all policies that expire a 120 days from now.Eg If the policy effectivedate is November 1 2012 and expiration date is November 1 2013 then on july first when I run a query this policy should be selected.
So you want to find all rows where the ExpirationDate is less than the current date + 120 days? Your question and your example are not quite the same. I will assume that my interpretation is correct. This is really not too difficult by using DATEADD.
Where ExpirationDate < dateadd(day, 120, getdate())
Now I am reading your post again and it is more confusing. If you add 120 days to July 1st you do NOT get November 1st, it is October 29th. Do you really mean 3 months instead of 120 days?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 2, 2013 at 2:52 pm
I really meant 120 days instead of 3 months.
July 2, 2013 at 3:22 pm
f9c882q 61644 (7/2/2013)
I really meant 120 days instead of 3 months.
So the expiration data in your example would NOT show up because it is further than 120 days. However, the code I posted will find those that expire within the next 120 days.
Hope that helps.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 3, 2013 at 7:14 am
I tried your code and it works perfectly, Thanks.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply