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

Calculating date in time. Expand / Collapse
Author
Message
Posted Tuesday, July 2, 2013 1:56 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 10, 2013 8:35 AM
Points: 20, Visits: 151
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.
Post #1469733
Posted Tuesday, July 2, 2013 2:01 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:43 PM
Points: 4,171, Visits: 3,201
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.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1469736
Posted Tuesday, July 2, 2013 2:18 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 13,282, Visits: 12,116
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1469741
Posted Tuesday, July 2, 2013 2:52 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 10, 2013 8:35 AM
Points: 20, Visits: 151
I really meant 120 days instead of 3 months.
Post #1469755
Posted Tuesday, July 2, 2013 3:22 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 13,282, Visits: 12,116
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1469772
Posted Wednesday, July 3, 2013 7:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 10, 2013 8:35 AM
Points: 20, Visits: 151
I tried your code and it works perfectly, Thanks.
Post #1470041
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse