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)
 Scalar Func questions

Author  Topic 

Trudye
Posting Yak Master

169 Posts

Posted - 2008-06-06 : 11:51:01
Hey Guys, I am creating a scalar function for the first time. I have googled, ck'd your FAQ's, looked 2 text books but cannot find the answer to my questions.
Is the IF statement used in the Scalar function like it is in the stored procedure. In other words does it have to return a Boolean? I want to perform a loop after I call the function, do I have to use a Cursor? Or can I code it like a VB function with a FOR/DO While? Also I can call this UDF from my Stored Proc, right?

Here is a quick synopsis of what I have been assigned to do:
--- Calculate Run Dates
-- (start from Previous Saturday subtract 41 days
-- if the 41st day does not fall on a Saturday (day = 7)
-- keep subtracting days until Day=Saturday. Add 6 days to make
-- END date the following Friday)

Thanx,
Trudye

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-06 : 11:53:46
IF statement is used exactly as in stored procedure. Can i see the code you tried till now?
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-06-06 : 12:39:48
Visakh16, thanks so much for responding. I don't have much I'm still trying to figure out how to attack the problem. I started writing a Stored Proc but then thought (in my infinite wisdom (lol)) maybe I should make this a UDF. To show you how Un-smart (is that a word?) that is I just started writing Stored Procs 2 months ago. Anywho here goes:

SET @Processing_Date = (REPLACE(REPLACE(CONVERT(varchar(8), getdate(), 112),'-',''),' ',''))
-- This runs on a Sunday, insure your processing date refelcts the prev Saturday
IF SELECT DatePart(dd,(DateAdd (Day, -1, @Processing_Date))) = 7
BEGIN
IF SELECT DatePart(dd, (DateAdd(Day, -41, @Processing_Date))) = 7
SET @StartDate = DateAdd(Day, -41, @Processing_Date)
ELSE
-- Call UDF to Subtract one from @Processing_Date until it = Saturday
Thanks again,
Trudye
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-06 : 12:52:16
Something like this should work (not tested)

CREATE FUNCTION dbo.ReturnDates
RETURNS @Dates Table
(StartDate datetime,
EndDate datetime
)
AS
BEGIN
DECLARE @PrevSat datetime,@StartDate datetime,@EndDate datetime

SET @PrevSat= DATEADD(dd,(-1) * DATEPART(dw,GETDATE()),GETDATE())
SET @StartDate= CASE WHEN DATEPART(dw,DATEADD(dd,-41,@PrevSat)) =7
THEN DATEADD(dd,-41,@PrevSat) ELSE DATEADD(dd,(-1) * DATEPART(dw,DATEADD(dd,-41,@PrevSat)),DATEADD(dd,-41,@PrevSat))

SET @EndDate = DATEADD(dd,6,@StartDate)

INSERT INTO @Dates
SELECT @StartDate,@EndDate

RETURN
END
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-06-06 : 13:13:30
Visakh16, thanks for responding so quickly.
It looks great but I’m not sure what it’s doing. What is the * in the DateAdd func for? What happens if the
-41 days lands you on a Wednesday. How do you loop backwards until you get to the previous Saturday to get the start date? The -41 is not absolute it has to be at least 41 days (i.e. the first Saturday prior to the -41st day). I’m so sorry for the confusion. Or maybe I’m waaaay off base and that’s what you code is doing.

OBTW I just found out I can use a For/While statement. Would that get me what I want?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-06 : 13:21:02
quote:
Originally posted by Trudye

Visakh16, thanks for responding so quickly.
It looks great but I’m not sure what it’s doing. What is the * in the DateAdd func for? What happens if the
-41 days lands you on a Wednesday. How do you loop backwards until you get to the previous Saturday to get the start date? The -41 is not absolute it has to be at least 41 days (i.e. the first Saturday prior to the -41st day). I’m so sorry for the confusion. Or maybe I’m waaaay off base and that’s what you code is doing.

OBTW I just found out I can use a For/While statement. Would that get me what I want?



* is multiplication operator

I'm finding out previous saturday in beginning. This is obtained by subtracting the weekday value from current date. If today is Friday, then weekday value will give you 6. then subtracting 6 days from current date will get us to Thurs,Wed,Tue,Mon,Sun,Sat and we will get prev sat. I will then subtract 41 days and find weekday. If its a wednesday say then week day will give us 4 subtracting 4 from wednesday gives Tue,Mon,Sun,Sat which will be the previous Sat. i take that as StartDate and add 6 days to get next Friday. Hope this is what you were looking for.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-06-06 : 13:33:29
[code]

select
MyDate,
[41 Days Back] = dateadd(dd,-41,a.MyDate),
Last_Saturday = dateadd(dd,((datediff(dd,-53685,dateadd(dd,-41,a.MyDate)+7)/7)*7)-7,-53685)
from
(
-- Test Data
select MyDate = convert(datetime,'20080601') union all
select MyDate = convert(datetime,'20080602') union all
select MyDate = convert(datetime,'20080603') union all
select MyDate = convert(datetime,'20080604') union all
select MyDate = convert(datetime,'20080605') union all
select MyDate = convert(datetime,'20080606') union all
select MyDate = convert(datetime,'20080607') union all
select MyDate = convert(datetime,'20080608') union all
select MyDate = convert(datetime,'20080609') union all
select MyDate = convert(datetime,'20080610') union all
select MyDate = convert(datetime,'20080611') union all
select MyDate = convert(datetime,'20080612') union all
select MyDate = convert(datetime,'20080613') union all
select MyDate = convert(datetime,'20080614')
) a


Results:

MyDate 41 Days Back Last_Saturday
----------------------- ----------------------- -----------------------
2008-06-01 00:00:00.000 2008-04-21 00:00:00.000 2008-04-19 00:00:00.000
2008-06-02 00:00:00.000 2008-04-22 00:00:00.000 2008-04-19 00:00:00.000
2008-06-03 00:00:00.000 2008-04-23 00:00:00.000 2008-04-19 00:00:00.000
2008-06-04 00:00:00.000 2008-04-24 00:00:00.000 2008-04-19 00:00:00.000
2008-06-05 00:00:00.000 2008-04-25 00:00:00.000 2008-04-19 00:00:00.000
2008-06-06 00:00:00.000 2008-04-26 00:00:00.000 2008-04-26 00:00:00.000
2008-06-07 00:00:00.000 2008-04-27 00:00:00.000 2008-04-26 00:00:00.000
2008-06-08 00:00:00.000 2008-04-28 00:00:00.000 2008-04-26 00:00:00.000
2008-06-09 00:00:00.000 2008-04-29 00:00:00.000 2008-04-26 00:00:00.000
2008-06-10 00:00:00.000 2008-04-30 00:00:00.000 2008-04-26 00:00:00.000
2008-06-11 00:00:00.000 2008-05-01 00:00:00.000 2008-04-26 00:00:00.000
2008-06-12 00:00:00.000 2008-05-02 00:00:00.000 2008-04-26 00:00:00.000
2008-06-13 00:00:00.000 2008-05-03 00:00:00.000 2008-05-03 00:00:00.000
2008-06-14 00:00:00.000 2008-05-04 00:00:00.000 2008-05-03 00:00:00.000

(14 row(s) affected)




[/code]

CODO ERGO SUM
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-06-09 : 14:05:08
Thanks Guys for your responses. Both responses are very slick and I would never have been able to come up with them on my own. I will add them to my library.

viaskh16, I noticed you used a UDF, is there a reason for that? I wrote a stored proc to calc the date. My only problem is I have never called a stored proc from within a stored proc, not sure how it works. I guess I rewrite it and make it a UDF.

But before I did that I thought I would ask what the advantages/disadvantages are? Why you prefer a UDF over a stored proc?

I need to call my DateProc from my Import_Export_Proc, have it return the @start_date and @end_date. Both dates are calc'd in the DateProc, nothing need to be passed into the DateProc. Any ideas how I do that?

Thanks again Guys,
Trudye
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-09 : 14:11:16
quote:
Originally posted by Trudye

Thanks Guys for your responses. Both responses are very slick and I would never have been able to come up with them on my own. I will add them to my library.

viaskh16, I noticed you used a UDF, is there a reason for that? I wrote a stored proc to calc the date. My only problem is I have never called a stored proc from within a stored proc, not sure how it works. I guess I rewrite it and make it a UDF.

But before I did that I thought I would ask what the advantages/disadvantages are? Why you prefer a UDF over a stored proc?

I need to call my DateProc from my Import_Export_Proc, have it return the @start_date and @end_date. Both dates are calc'd in the DateProc, nothing need to be passed into the DateProc. Any ideas how I do that?

Thanks again Guys,
Trudye



The main reason for using UDF was the you can call and use them wherever you want i.e they are reusable.You can even use them in select statement to get the dates.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-09 : 14:13:04
quote:
Originally posted by Trudye

Thanks Guys for your responses. Both responses are very slick and I would never have been able to come up with them on my own. I will add them to my library.

viaskh16, I noticed you used a UDF, is there a reason for that? I wrote a stored proc to calc the date. My only problem is I have never called a stored proc from within a stored proc, not sure how it works. I guess I rewrite it and make it a UDF.

But before I did that I thought I would ask what the advantages/disadvantages are? Why you prefer a UDF over a stored proc?

I need to call my DateProc from my Import_Export_Proc, have it return the @start_date and @end_date. Both dates are calc'd in the DateProc, nothing need to be passed into the DateProc. Any ideas how I do that?

Thanks again Guys,
Trudye



You can get values from udf as

SELECT StartDate,EndDate FROM dbo.ReturnDates()
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-06-09 : 14:44:01
visakh16, did I miss something. I entered this:
SELECT StartDate,EndDate FROM dbo.ReturnDates()
Print 'Returned Values'
Print @StartDate
Print @EndDate

Don't I need to define the parms in dbo.ReturnDates, like you do when you call a func in VB/VBA? Either that or make them global values?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-09 : 14:48:26
quote:
Originally posted by Trudye

visakh16, did I miss something. I entered this:
DECLARE @StartDate datetime,@EndDate datetime
SELECT @StartDate=StartDate,@EndDate=EndDate FROM dbo.ReturnDates()
Print 'Returned Values'
Print @StartDate
Print @EndDate

Don't I need to define the parms in dbo.ReturnDates, like you do when you call a func in VB/VBA? Either that or make them global values?

Go to Top of Page
   

- Advertisement -