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 DATETIMEASBEGIN RETURN (SELECT closedate from dbo.vw_MarketMailDates where month(inhomedate) = month(@inhome) and Market = @Market)ENDI 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