Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Further help on calculating months

Author  Topic 

ams006
Yak Posting Veteran

55 Posts

Posted - 2009-04-02 : 05:12:13
Hi all,

I'm trying to calculate the month between 2 specified dates and I'm using the following script:

select
case status
when 'live' then DATEDIFF(month, live_date, getdate())
else
DATEDIFF(month, live_date, exit_actioned_date)
end
AS Number_of_Months_in_Force,
*
from
policy


I've been reading the info in the link below regarding datediff not working as it is expected to. I've created the function but just need to know how to apply it to my script.

Any pointers would be appreciated.

[url]http://www.sqlteam.com/article/datediff-function-demystified[/url]

Thanks in advance

ams006

theboyholty
Posting Yak Master

226 Posts

Posted - 2009-04-02 : 09:22:09
I'm assuming the function works and you're just trying to implement it. .

If you've built your function and called it, say, 'Months_in_Force', you'll need to pass it three parameters: status, live_date and exit_actioned_date.

Perhaps something like this:

SELECT
Months_in_Force(status,live_date,exit_actioned_date) as Months_in_Force

FROM
policy

Note: You may need to put 'dbo.' before your function name i.e.
SELECT dbo.Months_in_Force(status,live_date,exit_actioned_date)
Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2009-04-02 : 09:23:21
Sorry, what I should have said is you may need to put the schema name. I'm just used to it always being 'dbo', but it could be something different, depends on your set-up.
Go to Top of Page
   

- Advertisement -