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 2000 Forums
 SQL Server Development (2000)
 looking to create a loop within function

Author  Topic 

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2010-02-11 : 12:00:23
I currently have a function that returns to me closedate from too variables. I am looking to also create a function to to return the previous closedate from the same two variables.

My current function is -


CREATE FUNCTION save_GetCloseDate
(
@Inhome datetime,
@Market varchar(6)
)
RETURNS DATETIME
AS
BEGIN
RETURN (SELECT closedate
from dbo.vw_MarketMailDates
where month(inhomedate) = month(@inhome) and Market = @Market)
END


I basically need the same thing but want what is like a FOR LOOP where it does
a (i=-1) month(dateadd(m, i, inhomedate))
and if the value is null (IE - no value in dbo.vw_MarketMailDates) to run
month(dateadd(m, i, inhomedate)) again with i=-2 etc and once it returns a value stop.

Could anyone help with this within SQL? Thanks


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-11 : 12:10:15
why do you need loop? wont this be enough?

CREATE FUNCTION save_GetCloseDate
(
@Inhome datetime,
@Market varchar(6)
)
RETURNS DATETIME
AS
BEGIN
RETURN (SELECT TOP 1 closedate
from dbo.vw_MarketMailDates
where inhomedate < = @inhome
and Market = @Market
ORDER BY inhomedate DESC)
END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2010-02-11 : 12:19:19
That is true. I focused on using in month() prior to changing and to passing in a date and using month-1. but that way makes sense and works. thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-11 : 12:25:32
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -